当前位置: 首页 > news >正文

vue使用谷歌地图实现地点查询

效果
在这里插入图片描述
在这里插入图片描述
代码
首先在index.html中引入谷歌地图资源

 <script src="https://maps.googleapis.com/maps/api/js?key='你的api密钥'&libraries=places"></script>

页面中

<template><div class="pac-card div-style" id="pac-card"><div id="map" class="flex-item1"></div><div id="pac-container" class="flex-item2"><el-inputv-model="localValue"ref="autocompleteInput"type="text"placeholder="输入地址"@input="handleInput"/><ulv-if="predictions.length != 0 && localValue != ''"class="autocomplete-list"><listyle="list-style: none"v-for="prediction in predictions":key="prediction.place_id"@click="selectPrediction(prediction)">{{ prediction.description }}</li></ul></div></div>
</template><script>
export default {name: "addressMap",data() {return {localValue: "",map: "",marker: "",longitude: -73.98633,latitude: 40.749933,predictions: [], // 存储搜索框提示的预测结果};},methods: {selectPrediction(prediction) {this.localValue = prediction.description;this.predictions = [];const that = this;// 创建 PlacesService 对象const placesService = new google.maps.places.PlacesService(map);// 获取地点的 Place IDconst placeId = prediction.place_id;// 发起 Places API 请求placesService.getDetails({ placeId: placeId }, function (place, status) {if (status === google.maps.places.PlacesServiceStatus.OK) {// 获取地点的经纬度坐标that.latitude = place.geometry.location.lat();that.longitude = place.geometry.location.lng();that.initMap();} else {alert("无法找到该地点!");}});},handleInput() {const autocompleteService = new google.maps.places.AutocompleteService();autocompleteService.getPlacePredictions({ input: this.localValue },(predictions, status) => {if (status === google.maps.places.PlacesServiceStatus.OK) {this.predictions = predictions;} else {this.predictions = [];}});},initMap() {const map = new google.maps.Map(document.getElementById("map"), {center: { lat: this.latitude, lng: this.longitude },zoom: 13,mapTypeControl: false,});},},mounted() {this.initMap();},
};
</script><!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
#map {overflow: hidden;width: 300px;height: 400px;margin: 0;
}
.div-style {display: flex;
}
.flex-item1 {flex: 2;
}
.flex-item2 {flex: 1;
}
.autocomplete-list {height: 200px;overflow: auto;ul {li {margin: 10px 0;}}
}
</style>
http://www.lryc.cn/news/163937.html

相关文章:

  • 前端该了解的网络知识
  • python3在虚拟环境实用vscode调试错误输出ModuleNotFoundError: No module named ‘django‘解决方法
  • 如何获得一个Oracle 23c免费开发者版
  • 机器学习策略二——优化深度学习系统
  • Pytorch Advanced(三) Neural Style Transfer
  • 英飞凌TC3xx--深度手撕HSM安全启动(三)--TC3xx HSM系统架构
  • 黑马JVM总结(五)
  • C语言入门Day_18 判断和循坏的小结
  • mac 好用的工具推荐
  • 星际争霸之小霸王之小蜜蜂(十二)--猫有九条命
  • 【软件分析/静态分析】chapter8 课程11/12 指针分析—上下文敏感(Pointer Analysis - Context Sensitivity)
  • 时间复杂度与空间复杂度详解
  • 目录操作函数
  • PlantUML入门教程:画时序图
  • C#范围运算符
  • 云数据库知识学习——云数据库产品、云数据库系统架构
  • C++中引用详解!
  • VUE3+TS项目无法找到模块“../version/version.js”的声明文件
  • 数据结构-堆的实现及应用(堆排序和TOP-K问题)
  • Spring 条件注解没生效?咋回事
  • 96. 不同的二叉搜索树
  • Android Jetpack 中Hilt的使用
  • 批量采集的时间管理与优化
  • uniApp监听左右滑动事件
  • 十八、MySQL添加外键?
  • 图像文件的操作MATLAB基础函数使用
  • 【k8s】Kubernetes版本v1.17.3 kubesphere 3.1.1 默认用户登录失败
  • Mysql加密功能
  • redis-win10安装和解决清缓存报错“Error: Protocol error, got “H“ as reply type byte”
  • 【视觉检测】电源线圈上的导线弯直与否视觉检测系统软硬件方案