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

news2024/9/22 11:34:51

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

在这里插入图片描述

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">
        <div
          v-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 = window
  if (windowTemp.google) {
    const temp: any = windowTemp.google
    googleMap = 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_address
          latitude.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 = window
  const temp: any = windowTemp.google
  const autocompleteService = new temp.maps.places.AutocompleteService()
  showpredictionsList.value = true
  autocompleteService.getPlacePredictions(
    { input: searchQuery.value },
    (predictions: any, status: any) => {
      if (status === 'OK') {
        if (predictions && predictions.length > 0)
          predictionsList.value = predictions
        else
          predictionsList.value = []
      }
    },
  )
}

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

// 设置地址
function selectPrediction(prediction: any) {
  //   // 创建 PlacesService 对象
  const windowTemp: any = window
  const temp: any = windowTemp.google
  const placesService = new temp.maps.places.PlacesService(googleMap)
  // 获取地点的 Place ID
  const 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.description
      showpredictionsList.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.features
  console.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 = tempOne
  console.log('🚀 ~ 第三层数据构造:', tempOne)
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1927383.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

指针!!C语言(第一篇)

指针1 指针变量和地址1.取地址操作符(&)2.指针变量和解引用操作符(*) 指针变量的大小和类型指针的运算特殊指针1.viod*指针2.const修饰指针3.野指针 assert断言指针的使用和传址调用1.strlen的模拟实现2.传值调用和传址调用 指针变量和地址 在认识指针之前&#xff0c;我们…

鸿蒙实训笔记

第一天 #初始化一个新的NPM项目(根据提示操作) npm init #安装TSC、TSLint和NodeJS的类型声明 npm install -s typescript tslint types/node 在根目录中新建一个名为tsconfig.json的文件&#xff0c;然后在代码编辑器中打开&#xff0c;写入下述内容&#xff1a; {"co…

SpringBoot+Vue实现简单的文件上传(Excel篇)

SpringBootVue实现简单的文件上传 1 环境 SpringBoot 3.2.1&#xff0c;Vue 2&#xff0c;ElementUI 2 页面 3 效果&#xff1a;只能上传xls文件且大小限制为2M&#xff0c;选择文件后自动上传。 4 前端代码 <template><div class"container"><el…

性能测试(2)

jmeter参数化 loadrunner Jmeter IP欺骗&#xff0c;也称为IP欺诈&#xff0c;是指通过伪装、篡改IP地址的方式&#xff0c;进行网络攻击或欺骗行为。这种行为可能会导致网络安全问题&#xff0c;包括身份盗窃、数据泄露、DDoS攻击等。为了保护自己的网络安全&#xff0c;用户…

5.3 需求分析

软件需求 定义 分类 真题 需求工程 需求获取 真题 需求分析 状态转换图 数据流图 数据流图分层 顶层数据流图、0层数据流图 1层数据流图 真题 需求规约 需求定义方法 需求验证 需求验证内容 需求管理 版本控制 需求跟踪 变更控制 真题

mysql不初始化升级

1、下载mysql&#xff0c;下载地址&#xff1a;MySQL :: Download MySQL Community Server 2、解压下载好的mysql&#xff0c;修改配置文件的datadir指定目录为当前数据存储的目录 3、通过管理员cmd进入新版本mysql的bin目录&#xff0c; 然后执行命令安装mysql服务&#xff…

2024年山东省安全员B证证考试题库及山东省安全员B证试题解析

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年山东省安全员B证证考试题库及山东省安全员B证试题解析是安全生产模拟考试一点通结合&#xff08;安监局&#xff09;特种作业人员操作证考试大纲和&#xff08;质检局&#xff09;特种设备作业人员上岗证考试大…

LeetCode 441, 57, 79

目录 441. 排列硬币题目链接标签思路代码 57. 插入区间题目链接标签思路两个区间的情况对每个区间的处理最终的处理 代码 79. 单词搜索题目链接标签原理思路代码 优化思路代码 441. 排列硬币 题目链接 441. 排列硬币 标签 数学 二分查找 思路 由于本题所返回的 答案在区间…

通过 PPPOE 将 linux 服务器作为本地局域网 IPv4 外网网关

将 linux 服务器作为本地外网网关&#xff0c;方便利用 Linux 生态中的各种网络工具&#xff0c;对流量进行自定义、精细化管理… 环境说明 拨号主机&#xff1a;CentOS 7.9, Linux Kernel 5.4.257 拨号软件: rp-pppoe-3.11-7.el7.x86_64初始化 1、升级系统到新的稳定内核&a…

半年GMV狂飙166亿!酒水赛道正在崛起自播之路

从2022年开始&#xff0c;酒水以“兴趣”为核心的直播电商迎来爆发式增长。以抖音电商为例&#xff0c;2022年下半年整体销售额破百亿&#xff0c;环比增幅超100%&#xff0c;2023年全年更是破300亿大关&#xff01;兴趣电商成为酒行业的第二增长曲线。 今年上半年&#xff0c;…

机器学习第四十七周周报 CF-LT

文章目录 week47 CF-LT摘要Abstract1. 题目2. Abstract3. 网络结构3.1 CEEMDAN&#xff08;完全自适应噪声集合经验模态分解&#xff09;3.2 CF-LT模型结构3.3 SHAP 4. 文献解读4.1 Introduction4.2 创新点4.3 实验过程 5. 结论6.代码复现小结参考文献 week47 CF-LT 摘要 本周…

可视耳勺是不是智商税?五款好用挖耳勺推荐!

随着人们追求健康生活方式的需求日益增长&#xff0c;可视耳勺这一产品逐渐走入了人们的视野&#xff0c;并受到了广泛的青睐。 然而&#xff0c;市场上可视耳勺品牌和种类繁多&#xff0c;部分产品存在清晰度不高、亮度较暗等问题&#xff0c;在使用过程很容易存在损坏耳道的风…

负载箱如何帮助维持电气系统的最佳性能

负载箱在维持电气系统最佳性能方面发挥着至关重要的作用&#xff0c;以下是负载箱如何帮助维持电气系统最佳性能的详细分析&#xff1a; 一、保护电气设备 负载箱能够在电气系统中产生恒定的负载&#xff0c;使电气设备在正常工作状态下运行。这避免了因负载波动过大而导致的…

数据库管理1

数据库管理 数据库运维。 sql语句 数据库用来增删改查的语句 备份 数据库的数据进行备份 主从复制&#xff0c;读写分离&#xff0c;高可用。 数据库的概念和相关的语法和规范&#xff1a; 数据库&#xff1a;组织&#xff0c;存储&#xff0c;管理数据的仓库。 数据库的管理系…

FreeRTOS_定时器

定时器概述 定时器运行过程 定时器就像一个闹钟&#xff0c;它有超时时间、函数、是否为周期性这三个部分。 超时时间&#xff1a;什么时候到时间&#xff0c;就像闹钟响起函数&#xff1a;闹钟响起&#xff0c;要干什么是否为周期性&#xff1a;这个闹钟只响一次&#xff0…

杭州高校大学智能制造实验室数字孪生可视化系统平台建设项目验收

杭州高校大学智能制造数字孪生技术作为智能制造的重要支撑&#xff0c;通过构建虚拟世界的镜像&#xff0c;实现对物理世界的实时监控、预测和优化。杭州高校大学智能制造实验室数字孪生可视化系统平台建设项目&#xff0c;旨在通过引入先进的数字孪生技术&#xff0c;构建一个…

刚刚,Claude国内可直连!

刚刚&#xff0c;Claude国内可直连&#xff01; 大家好&#xff01;我是科技博主Maynor。今天我们要分享一个激动人心的消息&#xff1a;知名AI助手Claude现在可以在国内直接访问了&#xff01; 这对于科技爱好者、学生、研究人员和各行各业的专业人士来说都是一个重大利好。…

进嵌入式公司当学徒没工资去不去啊?

在开始前分享一些嵌入式资料需要的同学评论888即可拿走 是我根据网友给的问题精心整理的 嵌入式是做技术岗位。技术不到位&#xff0c;没有人会要你。技术都没有&#xff0c;还要你的&#xff0c;一定是骗子。记住这句话就行了。 世上哪有那么好的事情。能免费让你学到真正有…

线性代数|机器学习-P24加速梯度下降(动量法)

文章目录 1. 概述2. 引入3. 动量法梯度下降 1. 概述 我们之前学的最速梯度下降[线搜索方法] 公式如下&#xff1a; x k 1 x k − s k ∇ f ( x k ) \begin{equation} x_{k1}x_k-s_k\nabla f(x_k) \end{equation} xk1​xk​−sk​∇f(xk​)​​ 但对于这种方法来说&#xff…

debian固定ip

debian固定ip 前言 安装好的Debian系统后&#xff0c;为了确保每次登陆的ip不变&#xff0c;需要固定 方法 命令如下 ip addr | grep inet因为有有线网和无线网 2 种连接方式&#xff0c;因此需要区别。 其中 enp 的是有线&#xff0c;wlp 的是无线 查看网关 IP 命令如下 …