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

vue3中谷歌地图+外网申请-原生-实现地址输入搜索+点击地图获取地址回显 +获取国外的geoJson实现省市区级联选择

一. 效果:输入后显示相关的地址列表,选中后出现标示图标和居中定位

在这里插入图片描述

1.初始化谷歌地图 在index.html加上谷歌api请求库

在这里插入图片描述

    <script src="https://maps.googleapis.com/maps/api/js?key=申请到的谷歌地图密钥&v=weekly&libraries=geometry,places,marker" defer></script>

注意:libraries=geometry,places,marker 是支持的功能插件,不加有些功能不支持

2.页面上加载

  <a-modal v-model:visible="openPopupTemp" force-render title="详细地址" width="1000px" @ok="handleOk" @cancel="handleCancel"><a-space direction="vertical" size="large" class="w-full" style="position: relative; margin-bottom: 20px;"><a-space><a-input v-model:value="searchQuery" placeholder="请输入地点名称" style="width: 600px" @input="handleInput" /><a-button type="primary" @click="handleInput">搜索</a-button></a-space><div v-if="predictionsList.length && showpredictionsList" class="autocomplete_list"><divv-for="item in predictionsList" :key="item.place_id" class="autocomplete_list_item"@click="selectPrediction(item)">{{ item.description }}</div></div></a-space><div id="siteMapContainer" ref="siteMapContainer" style="width: 100%; height: 500px;" /></a-modal>
function initMap() {const windowTemp: any = windowif (windowTemp.google) {const temp: any = windowTemp.googlegoogleMap = new temp.maps.Map(siteMapContainer.value, {center: { lat: latitude.value, lng: longitude.value },zoom: 8,mapId: 'DEMO_MAP_ID', // 需要id才能使用masker})temp.maps.event.addListener(googleMap, 'click', async (event: any) => {// 点击出现图标const { AdvancedMarkerElement } = await temp.maps.importLibrary('marker')Marker && Marker.setMap(null)Marker = new AdvancedMarkerElement({position: event.latLng,map: googleMap,})// 获取详细的地址const Geocoder = new temp.maps.Geocoder(googleMap)Geocoder.geocode({'latLng': event.latLng,}, (results: any, status: any) => {if (results[0].formatted_address) {searchQuery.value = results[0].formatted_addresslatitude.value = event.latLng.lat()longitude.value = event.latLng.lng()showpredictionsList.value = false}})})}
}

注意:

  • mapId: ‘DEMO_MAP_ID’, // 需要id才能使用AdvancedMarkerElement 图标功能
  • ant-modal需添加 force-render 属性才能够获取到siteMapContainer的dom元素,不然会报错!

3.输入查询服务

// 查询地址
function handleInput() {const windowTemp: any = windowconst temp: any = windowTemp.googleconst autocompleteService = new temp.maps.places.AutocompleteService()showpredictionsList.value = trueautocompleteService.getPlacePredictions({ input: searchQuery.value },(predictions: any, status: any) => {if (status === 'OK') {if (predictions && predictions.length > 0)predictionsList.value = predictionselsepredictionsList.value = []}},)
}

4.列表点击设置地址并显示图标+居中定位

// 设置地址
function selectPrediction(prediction: any) {//   // 创建 PlacesService 对象const windowTemp: any = windowconst temp: any = windowTemp.googleconst placesService = new temp.maps.places.PlacesService(googleMap)// 获取地点的 Place IDconst placeId: any = prediction.place_id//   发起 Places API 请求placesService.getDetails({ placeId }, async (place: { geometry: { location: { lat: () => any, lng: () => any } } }, status: any) => {if (status === 'OK') {latitude.value = place.geometry.location.lat()longitude.value = place.geometry.location.lng()const { AdvancedMarkerElement } = await temp.maps.importLibrary('marker')Marker && Marker.setMap(null)Marker = new AdvancedMarkerElement({position: place.geometry.location,map: googleMap,})searchQuery.value = prediction.descriptionshowpredictionsList.value = false// 定位googleMap.setCenter(place.geometry.location)}})
}

待改进点:

  1. 点击查询的地址,服务返回的不够详细,需要找找其他服务
  2. 谷歌地图需要外网才能使用

二.外网申请

在这里插入图片描述
使用这个获取软件
https://tagcloud.lanzoue.com/icE800yez8ah
使用这个获取外网账号
https://www.xfltd.net/dashboard
在这里插入图片描述

谷歌地图api文档
https://developers.google.cn/maps/documentation/geocoding?hl=zh-cn

三.获取国外的geoJson实现省市区级联选择

全球行政区划的数据库
https://docs.gmt-china.org/6.1/dataset/gadm/

在这里插入图片描述

四.处理国外的geoJson组合成为树结构,给级联组件使用

在这里插入图片描述
返回来的数据是 “NL_NAME_1”: 一层 “NL_NAME_2”: 二层 “NL_NAME_3”: 三层

解析二层方法

function getTwoTree() {const temp: any = []const features: any = gadm41_THA_2.features// console.log('🚀 ~ onMounted ~ features:', features)// 找出相同的省份features && features.forEach((item: any) => {temp.push(item.properties.NL_NAME_1)})const lastList: any = [...new Set(temp)]const myList: any = []for (let index = 0; index < lastList.length; index++) {const item = lastList[index]const children = features.filter((fItem: any) => item === fItem.properties.NL_NAME_1)const tempchildren: any = []children && children.forEach((cItem: any) => {tempchildren.push({value: cItem.properties.NL_NAME_2,label: cItem.properties.NL_NAME_2,})})myList.push({value: item,label: item,children: tempchildren,})}addrArrOptions.value = myList
}

解析三层方法

function getThreeTree() {const temp: any = []const features: any = gadm41_CHN_3.featuresconsole.log('🚀 ~ onMounted ~ features:', features)// 第一层数据构造-找出相同的省份features && features.forEach((item: any) => {temp.push(item.properties.NL_NAME_1)})const oneList: any = [...new Set(temp)]const tempOne: any = []oneList.forEach((item: any) => {tempOne.push({ children: [], value: item, label: item })})// 第二层数据构造for (let i = 0; i < tempOne.length; i++) {const childrenOne: any = features.filter((fItem: any) => tempOne[i].label === fItem.properties.NL_NAME_1)const temp: any = []childrenOne.forEach((cItem: any) => {if (!temp.find((tItem: any) => tItem.label === cItem.properties.NL_NAME_2)) {temp.push({label: cItem.properties.NL_NAME_2,value: cItem.properties.NL_NAME_2,})}})tempOne[i].children = temp}// 第三层数据构造for (let i = 0; i < tempOne.length; i++) {for (let j = 0; j < tempOne[i].children.length; j++) {const childrenTwo: any = features.filter((fItem: any) => tempOne[i].children[j].label === fItem.properties.NL_NAME_2)const temp: any = []childrenTwo.forEach((cItem: any) => {if (!temp.find((tItem: any) => tItem.label === cItem.properties.NL_NAME_3) && cItem.properties.NL_NAME_3 !== 'NA') {temp.push({label: cItem.properties.NL_NAME_3,value: cItem.properties.NL_NAME_3,})}})tempOne[i].children[j].children = temp}}addrArrOptions.value = tempOneconsole.log('🚀 ~ 第三层数据构造:', tempOne)
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 什么是软件定义安全SDSec
  • Archery 之SQL审核系统部署
  • MT6825磁编码IC在智能双旋机器人中的应用
  • 端到端拥塞控制的本质
  • CSS实现table表格:隔行换色的效果
  • 使用F1C200S从零制作掌机之debian文件系统完善NES
  • 电压反馈型运算放大器的增益和带宽
  • Delta的最新更新可让iPad用户同时模拟多款游戏
  • Redis6.2.1版本集群新加副本
  • Ubuntu 修改~/.bashrc终端选择是否使用annconda环境
  • 介绍一款数据准实时复制(CDC)中间件 `Debezium`
  • 绘画平台小程序的设计
  • mysql笔记1
  • 内存对齐的定义以及它的重要性
  • 路径:/EtherCATInfo/Descriptions/Devices/Device/Type/@ShowHideableSubDevices
  • __proto__ 和 prototype的关系
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • Angular数据绑定机制
  • angular学习第一篇-----环境搭建
  • Git初体验
  • HashMap剖析之内部结构
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • learning koa2.x
  • Linux链接文件
  • magento 货币换算
  • PHP的Ev教程三(Periodic watcher)
  • vuex 笔记整理
  • 翻译--Thinking in React
  • 关于List、List?、ListObject的区别
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 码农张的Bug人生 - 初来乍到
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 栈实现走出迷宫(C++)
  • 《天龙八部3D》Unity技术方案揭秘
  • 仓管云——企业云erp功能有哪些?
  • # Kafka_深入探秘者(2):kafka 生产者
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (06)金属布线——为半导体注入生命的连接
  • (1)bark-ml
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (55)MOS管专题--->(10)MOS管的封装
  • (C语言)球球大作战
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (十三)MipMap
  • (图文详解)小程序AppID申请以及在Hbuilderx中运行
  • (学习日记)2024.01.09
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (转)德国人的记事本
  • .mp4格式的视频为何不能通过video标签在chrome浏览器中播放?
  • .NET C# 配置 Options
  • .NET Core中的去虚