高德地图 Web 端开发详解:高德地图 API 最佳实践指南(安装、marker添加、逆向地理编码、实际业务案例实操)

高德地图 Web 端开发详解:高德地图 API 最佳实践指南(安装、marker添加、逆向地理编码、实际业务案例实操)

文章目录

1、引入高德地图的准备工作

1、成为 开发者

需要在这个网址上,注册高德开放平台 的 账号。 高德开放平台官网

2、创建应用

在这里插入图片描述

3、创建 Key

找到刚刚创建的应用,然后点击 添加Key, 选择 web端 js api ,这个地方,需要自己起一个名字,然后提交即可
在这里插入图片描述
成功提交 以后,会在刚刚创建的应用里面,显示出来对应的 key密钥
在这里插入图片描述

2、高德地图 JS API 使用方式

高德地图的加载方式有好几种,高德地图官方 JS API 引入方式

2.1 JS API Loader

这种方式是官方推荐的引入方式 ,这种方式主要分为以下俩种
2.1.1 使用 script 标签加载loader
<scriptsrc="https://webapi.amap.com/loader.js"></script><scripttype="text/javascript"> window._AMapSecurityConfig ={ securityJsCode:"「你申请的安全密钥」",}; AMapLoader.load({ key:"替换为你申请的 key",//申请好的 Web 端开发 Key,首次调用 load 时必填 version:"2.0",//指定要加载的 JS API 的版本,缺省时默认为 1.4.15 plugins:["AMap.Scale"],//需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['AMap.Scale','...','...'] AMapUI:{//是否加载 AMapUI,缺省不加载 version:"1.1",//AMapUI 版本 plugins:["overlay/SimpleMarker"],//需要加载的 AMapUI ui 插件}, Loca:{//是否加载 Loca, 缺省不加载 version:"2.0",//Loca 版本},}).then((AMap)=>{var map =newAMap.Map("container");//"container"为 <div> 容器的 id map.addControl(newAMap.Scale());//添加比例尺组件到地图实例上}).catch((e)=>{ console.error(e);//加载错误提示});</script>
2.1.2 NPM 安装loader
npm i @amap/amap-jsapi-loader --save 
这种方式更多常见于工程化项目中 ,下面演示的时候,也是使用这种方式进行安装 , 也可以使用 pnpm 都可以
import AMapLoader from"@amap/amap-jsapi-loader"; window._AMapSecurityConfig ={ securityJsCode:"「你申请的安全密钥」",}; AMapLoader.load({ key:"替换为你申请的 key",//申请好的 Web 端开发者 Key,首次调用 load 时必填 version:"2.0",//指定要加载的 JS API 的版本,缺省时默认为 1.4.15 plugins:["AMap.Scale"],//需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['AMap.Scale','...','...']}).then((AMap)=>{var map =newAMap.Map("container");//"container"为 <div> 容器的 id}).catch((e)=>{ console.log(e);});

2.2 script 标签加载 JS API 脚本

2.2.1 同步加载
<!-- 需要设置元素的宽高样式 --><divid="container"></div><scripttype="text/javascript"> window._AMapSecurityConfig ={ securityJsCode:"「你申请的安全密钥」",};</script><scripttype="text/javascript"src="https://webapi.amap.com/maps?v=2.0&key=你申请的key值"></script><scripttype="text/javascript">//地图初始化应该在地图容器 <div> 已经添加到 DOM 树之后var map =newAMap.Map("container",{ zoom:12,});</script>
2.2.2 异步加载
我们项目中就是用的这个方式,但是这个方式会出现略微的卡顿,因为浏览器要下载下来这个js 文件,然后解析
<script>//设置你的安全密钥 window._AMapSecurityConfig ={ securityJsCode:"「你申请的安全密钥」",};//声明异步加载回调函数 window.onLoad=function(){var map =newAMap.Map("container");//"container"为<div>容器的id};var url ="https://webapi.amap.com/maps?v=2.0&key=你申请的key值&callback=onLoad";var jsapi = document.createElement("script"); jsapi.charset ="utf-8"; jsapi.src = url; document.head.appendChild(jsapi);</script>

3、在 vue3 项目中使用

3.1 安装 js api loader

安装 npm 包
npm i @amap/amap-jsapi-loader --save 

3.2 在组件中使用

新建一个空白的 vue 组件,里面要写一个 div ,然后设置以下,ID ,然后处理以下这个DIV 的样式,要保证有高度,然后就引入,具体的代码如下
<template><divid="MapContainer"ref="mapContainerRef"></div></template><scriptsetup>import{ onMounted, onUnmounted, ref }from"vue";import AMapLoader from"@amap/amap-jsapi-loader";let map =null;const mapContainerRef =ref(null);onMounted(()=>{ console.log("mapContainerRef", mapContainerRef); window._AMapSecurityConfig ={ securityJsCode:"",// 「你申请的安全密钥」}; AMapLoader.load({ key:"",// 申请好的Web端开发者Key,首次调用 load 时必填 version:"2.0",// 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15}).then((AMap)=>{ map =newAMap.Map("MapContainer",{// map = new AMap.Map(mapContainerRef.value, { viewMode:"3D",// 是否为3D地图模式 zoom:11,// 初始化地图级别 center:[116.397428,39.90923],// 初始化地图中心点位置 defaultCursor:"pointer",});}).catch((e)=>{ console.log(e);});});onUnmounted(()=>{ map?.destroy();});</script><stylescoped>#MapContainer{width: 100%;height: 800px;}</style>

注意

new AMap.Map 的第一个参数,可以是 DOM元素或者 此元素的ID,在vue中你可以使用 ID 或者 ref 来操作这个
在这里插入图片描述
defaultCursor 这个属性是配置用户鼠标移入到地图上,显示的图标,就是 css 的 cursor: pointer;
在这里插入图片描述

4、实际应用

以下列举,我在实际开发中,遇到的一些需求点

4.1 点击地图获取经纬度、设置地图中心点、添加 marker、获取省市区及详细地址

需求:用户 点击地图上的一个后,要添加一个 定位的图标 ,然后 显示出来 用户点击的经纬度、省市区、详细地址。具体的效果看下面这个 GIF
在这里插入图片描述
4.1.1 准备变量

代码编写

先初始化一个对象,用来存放这些信息
let positionInfo =ref({ lng:0,// 经度 lat:0,// 纬度 provinceCode:"",// 省份编号 provinceName:"",// 省份名称 cityCode:"",// 市编号 cityName:"",// 市名称 countyCode:"",// 区编号 countyName:"",// 区名称 address:"",// 详细地址});
4.1.2 地图点击事件的监听
第一步,肯定是要先看看高德地图的api ,有没有鼠标点击事件,可以看到在高德地图的参考手册上 Map 是可以绑定点击事件的
在这里插入图片描述
第二步,代码编写,先新建一个 handleAMapClick 方法,然后在 地图初始化完成后,进行调用,便于下面演示其他的操作
在这里插入图片描述
/** * 处理用户点击 地图的点,就拿到经纬度、省市区、详细地址,并且添加 marker */functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();}}
其实这个点击事件,是有一个参数的 ,里面就有经纬度信息,在高德地图参考手册中,这个 lnglat 是非常常见的, lng 就是经度,lat 是纬度;高德地图点击事件官方文档
在这里插入图片描述
4.1.3 地图添加 marker
这个时候,其实已经是拿到了,经纬度信息,然后就是要添加一个标记点,,添加标记点,需要用到 AMap.Marker 方法;高德地图 marker 官方文档
functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker);});}
这个方法核心就是,你要先创建一个 marker ,然后吧 maker 添加到地图上。AMap.Marker方法接收一个对象,这个对象,最主要的参数就是 position ,需要传递一个 LngLat 类型的数据进去

Lnglat 类型

它是高德地图的一个基础类,具体的使用如下
newAMap.LngLat(lng: Number?, lat: Number?, noWrap: Boolean?)
第一个参数是经度,第二个参数是 纬度
在这里插入图片描述
这样的话,就已经 实现了, 添加 marker ,但是这个地图还存在一个问题,每次点击都会生成一个新的 marker ,需要每次点击的时候,把之前的 marker全部清除掉
在这里插入图片描述
4.1.4 地图清理 marker的方式
这个清除 marker 目前我发现有三种方式,分别如下

第一种方式

把这个地图的所有的 marker 都存起来,然后 挨个删除
map 实例上,getAllOverlays 需要接收一个参数(覆盖物的类型,比如:marker、circle、polyline、polygon),返回值是一个数组
map 实例上,remove 需要接收 一个或者多个 覆盖物,要么是一个数组,要么是一个覆盖物
在这里插入图片描述


在这里插入图片描述
// 这里的map 就是 new AMap.Map 的返回值const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));

第二种方式

暴力解决,直接把当前地图的所有覆盖物,全部删除
// 这里的map 就是 new AMap.Map 的返回值 map.clearMap();// 删除地图上所有的覆盖物

第三种方式

使用 Marker 对象的 remove 方法 ,这个方法存在一个缺点,你需要在添加 marker 以后,要找一个地方存储起来
// 这里的 marker 指的是, nwe AMap.Marker 的返回值 marker.remove()

第四种方式

使用 Marker 对象的 setMap 方法,
// 这里的 marker 指的是, nwe AMap.Marker 的返回值 marker.setMap(null)// 这里需要传递 null 
到目前位置代码如下
functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker);});}
4.1.5 调整地图中心点
在这里插入图片描述
用户点击后,这个 marker ,已经是到了屏幕的右下角,这个时候就需要调整地图的中心点,让用户始终感觉当前的 marker 在 正中心,这个地方,目前发现俩个处理方式,一个是 Map.setCenter ,另一个是Map.setZoomAndCenter ,但这个地方更建议使用 setCenter 因为另一个方法需要传递一个 zoom,就是地图的缩放等级
在这里插入图片描述
functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker); map.setCenter(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat));});}
在添加 marker 以后,再调 setCenter 方法即可,具体的效果如下
在这里插入图片描述
4.1.6 根据经纬度获取详细位置
设置好地图中心点以后,就要根据经纬度 获取 详细的地址
根据经纬度获取 详细地址 / 根据详细地址获取经纬度, 这两个操作在高德官方 api 文档上,称为正向地理编码逆向地理编码
  • 正向地理编码:详细地址 => 经纬度
  • 逆向地理编码:经纬度 => 详细地址
我们现在要用的就是 逆向地理编码
在这里插入图片描述

第一种方式

在控制台,可以输入 AMap ,高德地图会在window上挂在这个key
//引入插件,此示例采用异步引入,更多引入方式 https://lbs.amap.com/api/javascript-api-v2/guide/abc/plugins AMap.plugin("AMap.Geocoder",function(){var geocoder =newAMap.Geocoder({ city:"010",// city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode});var lnglat =[116.396574,39.992706]; geocoder.getAddress(lnglat,function(status, result){if(status ==="complete"&& result.info ==="OK"){// result为对应的地理位置详细信息 console.log(result);}});});

第二种方式

高德地图API AMap.Geocoder 文档
// 这里的 mapObj 就是创建的 Map 实例,也就是 new AMap.Map 的返回值var geocoder;//加载地理编码插件 mapObj.plugin(["AMap.Geocoder"],function(){//加载地理编码插件 geocoder =newAMap.Geocoder({ radius:1000,//以已知坐标为中心点,radius为半径,返回范围内兴趣点和道路信息 extensions:"all"//返回地址描述以及附近兴趣点和道路信息,默认“base”});//返回地理编码结果 geocoder.on("complete", geocoder_CallBack);//逆地理编码 geocoder.getAddress(newAMap.LngLat(116.359119,39.972121));});

第三种方式

高德地图api 逆向地理编码调用接口的方式获取
// 调这个接口,传入对应的参数 https://restapi.amap.com/v3/geocode/regeo?output=xml&location=116.310003,39.991957&key=<用户的key>&radius=1000&extensions=all 
这里,使用第二种方式,具体的代码如下
/** * 处理用户点击 地图的点,就拿到经纬度、省市区、详细地址,并且添加 marker */functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();// 这个地方,也需要吧原来的 marker 都清空// 第一种方式// map.clearMap(); // 删除地图上所有的覆盖物// 第二种方式const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker); console.log("marker", marker); map.setCenter(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat)); map.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),function(status, res){if(status ==="complete"&& res.info ==="OK"){// res 为对应的地理位置详细信息 console.log("地图:地图点击 逆向地理编码返回值", res); positionInfo.value.address = res.regeocode.formattedAddress; positionInfo.value.provinceCode = res.regeocode.addressComponent.adcode.slice(0,2); positionInfo.value.provinceName = res.regeocode.addressComponent.province; positionInfo.value.cityCode = res.regeocode.addressComponent.adcode.slice(2,4); positionInfo.value.cityName = res.regeocode.addressComponent.city || res.regeocode.addressComponent.province; positionInfo.value.countyCode = res.regeocode.addressComponent.adcode.slice(4,6); positionInfo.value.countyName = res.regeocode.addressComponent.district;}});});});}
逆向地理编码的 返回值如下
主要使用的就是 formattedAddressadcodeprovincecitydistrict ,这几个是最常用的,adcode 是行政区编码,其他值的含义在这个链接里面 逆向地理编码 返回值 解释
在这里插入图片描述
到这里就实现了,第一个需求

4.2 搜索地点 点击后获取所有信息、设置地图中心点、marker

在这里插入图片描述
4.2.1 增加输入框、变量
下面这个就是完成 4.1 之后,又添加 输入框的布局、变量,之后的代码
注意:这次使用的是 UI库是 ant-design-vue 4.x 版本
<template><divclass="MapPage"><a-rowclass="MapPage-search"><a-input-searchv-model:value="poiValue"placeholder="输入关键词"size="large"@search="handleSearchClick"/><divclass="MapPage-search-poi"><a-rowv-for="item in poiList":key="item.ID"style="cursor: pointer;margin-bottom: 5px"@click="handlePOIItemClick(item)"> {{ item.Name }}【{{ item.Address }}】 </a-row></div></a-row><divid="MapContainer"ref="mapContainerRef"></div><divclass="MapPage-footer"><a-row> 经度: {{ positionInfo.lng }} , 纬度: {{ positionInfo.lat }} </a-row><a-row>省份编号: {{ positionInfo.provinceCode }} 省份名称: {{ positionInfo.provinceName }} </a-row><a-row>市编号: {{ positionInfo.cityCode }} 市名称: {{ positionInfo.cityName }} </a-row><a-row>区编号: {{ positionInfo.countyCode }} 区名称: {{ positionInfo.countyName }} </a-row><a-row>地址: {{ positionInfo.address }} </a-row></div></div></template><scriptsetup>import{ onMounted, onUnmounted, ref }from"vue";import AMapLoader from"@amap/amap-jsapi-loader";let map =null;const mapContainerRef =ref(null);let positionInfo =ref({ lng:0, lat:0, provinceCode:"", provinceName:"", cityCode:"", cityName:"", countyCode:"", countyName:"", address:"",});let poiValue =ref("");let poiList =ref([]);onMounted(()=>{ console.log("mapContainerRef", mapContainerRef); window._AMapSecurityConfig ={ securityJsCode:"f22de8e155d91e514b61904b9b10e05a",// 「你申请的安全密钥」}; AMapLoader.load({ key:"6d4f7a678203e93f42c21145a3b16d43",// 申请好的Web端开发者Key,首次调用 load 时必填 version:"2.0",// 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15}).then((AMap)=>{ map =newAMap.Map("MapContainer",{// map = new AMap.Map(mapContainerRef.value, { viewMode:"3D",// 是否为3D地图模式 zoom:15,// 初始化地图级别 center:[116.397428,39.90923],// 初始化地图中心点位置 defaultCursor:"pointer",}); console.log("地图:实例", map);handleAMapClick();}).catch((e)=>{ console.log(e);});});onUnmounted(()=>{ map?.destroy();});/** * 处理用户点击 地图的点,就拿到经纬度、省市区、详细地址,并且添加 marker */functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();// 这个地方,也需要吧原来的 marker 都清空// 第一种方式// map.clearMap(); // 删除地图上所有的覆盖物// 第二种方式const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker); console.log("marker", marker); map.setCenter(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat)); map.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),function(status, res){if(status ==="complete"&& res.info ==="OK"){// res 为对应的地理位置详细信息 console.log("地图:地图点击 逆向地理编码返回值", res); positionInfo.value.address = res.regeocode.formattedAddress; positionInfo.value.provinceCode = res.regeocode.addressComponent.adcode.slice(0,2); positionInfo.value.provinceName = res.regeocode.addressComponent.province; positionInfo.value.cityCode = res.regeocode.addressComponent.adcode.slice(2,4); positionInfo.value.cityName = res.regeocode.addressComponent.city || res.regeocode.addressComponent.province; positionInfo.value.countyCode = res.regeocode.addressComponent.adcode.slice(4,6); positionInfo.value.countyName = res.regeocode.addressComponent.district;}});});});}</script><stylescoped>#MapContainer{width: 100%;height: 700px;}.MapPage-footer{padding: 0 20px;}.MapPage-search{position: relative;}.MapPage-search-poi{position: absolute;z-index: 2;top: 32px;width: 100%;background-color: #fff;}</style>
4.2.2 POI 搜索
这个 POI 就是 兴趣点的意思,也可以理解为 大多数人想搜索的 地点。官方的解释如下,关于POI 官方解释
在这里插入图片描述
这个POI 也是有多种使用方式

第一种

通过 window 上的 AMap.plugin 来获取POI
官方地址:https://lbs.amap.com/api/javascript-api-v2/guide/services/autocomplete
在这里插入图片描述


第二种

通过 地图实例的 plugin 进行加载
官方地址:https://lbs.amap.com/api/javascript-api-v2/documentation#placesearch
在这里插入图片描述


第三种

通过调接口的形式进行获取 POI
官方地址:https://lbs.amap.com/api/webservice/guide/api-advanced/search
在这里插入图片描述
POI 的返回值如下,这个返回值结构还是比较简单的,如若设置了 extensions: "all" 返回值就会变得复杂了
在这里插入图片描述
这个地方,我是用的是 第一种方式,但是这个方式好像是异步的,所以又封装了以下,这里的思路就是 根据POI 拿到对应的 经纬度,然后通过经纬度获取具体的地址信息
consthandleSearchClick=()=>{ console.log("地图:POI 关键字", poiValue.value); AMap.plugin("AMap.PlaceSearch",function(){var placeSearch =newAMap.PlaceSearch({ extensions:"base",// base | all ,base 是返回基本信息,all 是返回 完整信息}); placeSearch.search(poiValue.value,asyncfunction(status, res){//查询成功时, res 即对应匹配的 POI 信息 console.log("地图:POI 搜索返回值", status, res, res.poiList.pois);if(status ==="complete"&& res.info =="OK"){let formatList =[];for(const f of res.poiList.pois){let item ={}; item.ID= f.id; item.LngLat = f.location.lng +","+ f.location.lat; item.Name = f.name;// 根据经纬度 获取 详细地址let res =awaitgetAddressByLnglat([f.location.lng, f.location.lat]); item.Address = res.regeocode.formattedAddress; formatList.push(item);} poiList.value = formatList;}});});};/** * 处理 根据经纬度 获取 详细地址 * @param {Array} lnglat * @returns {Promise} res */functiongetAddressByLnglat(lnglat){returnnewPromise((resolve, reject)=>{ AMap.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(lnglat,function(status, result){if(status ==="complete"&& result.info ==="OK"){// result为对应的地理位置详细信息// item.Address = result.regeocode.formattedAddress;resolve(result);}});});});}
4.2.3 处理 点击每个POI 跳转
// 点击每一个 POI 的时候consthandlePOIItemClick=(item)=>{ console.log("地图:POI 点击事件", item, item.LngLat.split(",")); positionInfo.value.lng = item.LngLat.split(",")[0]; positionInfo.value.lat = item.LngLat.split(",")[1];const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker); map.setCenter(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat)); map.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),function(status, res){if(status ==="complete"&& res.info ==="OK"){// res 为对应的地理位置详细信息 positionInfo.value.address = res.regeocode.formattedAddress; positionInfo.value.provinceCode = res.regeocode.addressComponent.adcode.slice(0,2); positionInfo.value.provinceName = res.regeocode.addressComponent.province; positionInfo.value.cityCode = res.regeocode.addressComponent.adcode.slice(2,4); positionInfo.value.cityName = res.regeocode.addressComponent.city || res.regeocode.addressComponent.province; positionInfo.value.countyCode = res.regeocode.addressComponent.adcode.slice(4,6); positionInfo.value.countyName = res.regeocode.addressComponent.district;} poiList.value =[]; poiValue.value ="";});});};

6、完整代码

实现上面两个需求的完整代码如下
<template><divclass="MapPage"><a-rowclass="MapPage-search"><a-input-searchv-model:value="poiValue"placeholder="输入关键词"size="large"@search="handleSearchClick"/><divclass="MapPage-search-poi"><a-rowv-for="item in poiList":key="item.ID"style="cursor: pointer;margin-bottom: 5px"@click="handlePOIItemClick(item)"> {{ item.Name }}【{{ item.Address }}】 </a-row></div></a-row><divid="MapContainer"ref="mapContainerRef"></div><divclass="MapPage-footer"><a-row> 经度: {{ positionInfo.lng }} , 纬度: {{ positionInfo.lat }} </a-row><a-row>省份编号: {{ positionInfo.provinceCode }} 省份名称: {{ positionInfo.provinceName }} </a-row><a-row>市编号: {{ positionInfo.cityCode }} 市名称: {{ positionInfo.cityName }} </a-row><a-row>区编号: {{ positionInfo.countyCode }} 区名称: {{ positionInfo.countyName }} </a-row><a-row>地址: {{ positionInfo.address }} </a-row></div></div></template><scriptsetup>import{ onMounted, onUnmounted, ref }from"vue";import AMapLoader from"@amap/amap-jsapi-loader";let map =null;const mapContainerRef =ref(null);let positionInfo =ref({ lng:0, lat:0, provinceCode:"", provinceName:"", cityCode:"", cityName:"", countyCode:"", countyName:"", address:"",});let poiValue =ref("");let poiList =ref([]);onMounted(()=>{ console.log("mapContainerRef", mapContainerRef); window._AMapSecurityConfig ={ securityJsCode:"f22de8e155d91e514b61904b9b10e05a",// 「你申请的安全密钥」}; AMapLoader.load({ key:"6d4f7a678203e93f42c21145a3b16d43",// 申请好的Web端开发者Key,首次调用 load 时必填 version:"2.0",// 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15}).then((AMap)=>{ map =newAMap.Map("MapContainer",{// map = new AMap.Map(mapContainerRef.value, { viewMode:"3D",// 是否为3D地图模式 zoom:15,// 初始化地图级别 center:[116.397428,39.90923],// 初始化地图中心点位置 defaultCursor:"pointer",}); console.log("地图:实例", map);handleAMapClick();}).catch((e)=>{ console.log(e);});});onUnmounted(()=>{ map?.destroy();});/** * 处理用户点击 地图的点,就拿到经纬度、省市区、详细地址,并且添加 marker */functionhandleAMapClick(){ map.on("click",function(e){ console.log("地图:点击事件", e); positionInfo.value.lng = e.lnglat.getLng(); positionInfo.value.lat = e.lnglat.getLat();// 这个地方,也需要吧原来的 marker 都清空// 第一种方式// map.clearMap(); // 删除地图上所有的覆盖物// 第二种方式const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker); console.log("marker", marker); map.setCenter(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat)); map.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),function(status, res){if(status ==="complete"&& res.info ==="OK"){// res 为对应的地理位置详细信息 console.log("地图:地图点击 逆向地理编码返回值", res); positionInfo.value.address = res.regeocode.formattedAddress; positionInfo.value.provinceCode = res.regeocode.addressComponent.adcode.slice(0,2); positionInfo.value.provinceName = res.regeocode.addressComponent.province; positionInfo.value.cityCode = res.regeocode.addressComponent.adcode.slice(2,4); positionInfo.value.cityName = res.regeocode.addressComponent.city || res.regeocode.addressComponent.province; positionInfo.value.countyCode = res.regeocode.addressComponent.adcode.slice(4,6); positionInfo.value.countyName = res.regeocode.addressComponent.district;}});});});}/** * 处理 搜索按钮点击的时候 */consthandleSearchClick=()=>{ console.log("地图:POI 关键字", poiValue.value); AMap.plugin("AMap.PlaceSearch",function(){var placeSearch =newAMap.PlaceSearch({ extensions:"base",// base | all ,base 是返回基本信息,all 是返回 完整信息}); placeSearch.search(poiValue.value,asyncfunction(status, res){//查询成功时, res 即对应匹配的 POI 信息 console.log("地图:POI 搜索返回值", status, res, res.poiList.pois);if(status ==="complete"&& res.info =="OK"){let formatList =[];for(const f of res.poiList.pois){let item ={}; item.ID= f.id; item.LngLat = f.location.lng +","+ f.location.lat; item.Name = f.name;// 根据经纬度 获取 详细地址let res =awaitgetAddressByLnglat([f.location.lng, f.location.lat]); item.Address = res.regeocode.formattedAddress; formatList.push(item);} poiList.value = formatList;}});});};/** * 处理 根据经纬度 获取 详细地址 * @param {Array} lnglat * @returns {Promise} res */functiongetAddressByLnglat(lnglat){returnnewPromise((resolve, reject)=>{ AMap.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(lnglat,function(status, result){if(status ==="complete"&& result.info ==="OK"){// result为对应的地理位置详细信息// item.Address = result.regeocode.formattedAddress;resolve(result);}});});});}// 点击每一个 POI 的时候consthandlePOIItemClick=(item)=>{ console.log("地图:POI 点击事件", item, item.LngLat.split(",")); positionInfo.value.lng = item.LngLat.split(",")[0]; positionInfo.value.lat = item.LngLat.split(",")[1];const markers = map.getAllOverlays("marker");// 获取地图上的所有 marker markers.forEach((f)=> map.remove(f));let marker =newAMap.Marker({ position:newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),}); map.add(marker); map.setCenter(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat)); map.plugin("AMap.Geocoder",function(){let geocoder =newAMap.Geocoder({}); geocoder.getAddress(newAMap.LngLat(positionInfo.value.lng, positionInfo.value.lat),function(status, res){if(status ==="complete"&& res.info ==="OK"){// res 为对应的地理位置详细信息 positionInfo.value.address = res.regeocode.formattedAddress; positionInfo.value.provinceCode = res.regeocode.addressComponent.adcode.slice(0,2); positionInfo.value.provinceName = res.regeocode.addressComponent.province; positionInfo.value.cityCode = res.regeocode.addressComponent.adcode.slice(2,4); positionInfo.value.cityName = res.regeocode.addressComponent.city || res.regeocode.addressComponent.province; positionInfo.value.countyCode = res.regeocode.addressComponent.adcode.slice(4,6); positionInfo.value.countyName = res.regeocode.addressComponent.district;} poiList.value =[]; poiValue.value ="";});});};</script><stylescoped>#MapContainer{width: 100%;height: 700px;}.MapPage-footer{padding: 0 20px;}.MapPage-search{position: relative;}.MapPage-search-poi{position: absolute;z-index: 2;top: 32px;width: 100%;background-color: #fff;}</style>

7、总结

其实对于前端开发来说,最常用的就是 JS API ,但是这个地方,对于一些插件的介绍不完整,特别是 返回值,比如 POI 搜索的返回值逆向地理编码的返回值
还有一个比较常见,就是获取用户当前的位置,这个是要获取 读取位置权限的,然后拿到 经纬度,还是要通过 逆向地理编码 拿到具体的地址
在这里插入图片描述
常用的 链接,整理如下

Read more

【论文阅读】DSRL: Steering Your Diffusion Policy with Latent Space Reinforcement Learning

【论文阅读】Steering Your Diffusion Policy with Latent Space Reinforcement Learning * 1 团队与发表时间 * 2. 问题背景与核心思路 * 3. 具体做法 * 3.1 模型设计 * 3.2 Loss 设计 * 3.3 数据设计 * 4 实验效果 * 5 结论 * 6 扩散模型进行RL的方案 * 6.1 纯离线设置 (Purely Offline Setting) * 6.2 在线设置 (Online Setting) * 6.3 残差策略 (Residual Policy) 1 团队与发表时间

Pix4Dmapper处理大疆无人机影像数据教程

Pix4Dmapper处理大疆无人机影像数据教程

初次接触无人机数据处理时,我完全找不到清晰的流程指引,甚至对大疆采集的数据如何使用都毫无头绪。查阅了不少资料,发现信息也相当有限。为避免日后遗忘,特此记录下摸索出的操作流程,权当备忘。 1. 想要使用Pix4D软件的朋友请注意:这款软件需要付费购买。我查阅了网上资源,发现大多数人都没有提供免费版本。我已经购买了“正版”软件,有需要的朋友可以私信我,我会分享下载链接给你。 2. 结束,到这里 下面是软件处理影像过程 (1)、首先打开Pix4DTool,点击start或者Auto start以后,立马会将软件的网进行断开,这样就可以进行使用pix4d软件了。 (2)、此时打开软件的界面如下所示 (3)、拷贝数据到电脑然后打开软件新建项目输入项目名称并选好路径点击下一步 (4)、添加无人机照片路径或选择添加照片完成并点击下一步 (5)、因为精灵RTK照片自带POS信息这里就直接默认坐标系,相机参数是写入在照片里可以自动读取,如果不确定就用记事本打开照片找到XMP把相机信息参数输入点击下一步 (6)、输出坐标系选择自己需要的坐标系,和像控点一致的

FPGA Transformer加速完全指南:从模型优化到硬件实现(附实战案例)

🚀 FPGA Transformer加速完全指南:从模型优化到硬件实现(附实战案例) 📚 目录导航 文章目录 * 🚀 FPGA Transformer加速完全指南:从模型优化到硬件实现(附实战案例) * 📚 目录导航 * 概述 * 第一部分:Transformer基础与FPGA加速价值定位 * 1.1 Transformer架构概览 * 1.1.1 Transformer的基本结构 * 1.1.2 Transformer的关键特性 * 1.1.3 常见的Transformer变体 * 1.2 Transformer推理的挑战 * 1.2.1 计算复杂度分析 * 1.2.2 内存访问瓶颈 * 1.2.3 非线性操作的挑战 * 1.2.4 推理延迟分析 * 1.3

Science子刊超绝idea:注意力机制+强化学习!足式机器人障碍穿越首次达成 100% 成功率

Science子刊超绝idea:注意力机制+强化学习!足式机器人障碍穿越首次达成 100% 成功率

近期,注意力机制+强化学习这个方向迎来了重磅突破。苏黎世联邦理工学院机器人系统实验室在《Science Robotics》(IF=26.1)中提出了一种创新的控制框架: 该框架通过结合强化学习和多头注意力机制,让机器人在面对不同类型地形时,能做到精准判断和灵活适应,从而实现100%障碍穿越成功率! 值得一提的是,当前注意力机制+强化学习这个方向已从方法创新阶段进入了性能优化和应用拓展阶段,而这篇顶刊成果,正是该趋势在机器人控制领域的完美范例!对于想做这个方向的论文er说,属于必看文章! 当然这方向还有不少值得参考的成果,我已经帮大家筛选并整理了11篇高质量的文章,包含顶会顶刊,附代码,先学习一下前人的思路再入手,能高效地找到自己的idea。 全部论文+开源代码需要的同学看文末 ARiADNE: A Reinforcement learning approach using Attention-based Deep Networks for Exploration 关键词:Reinforcement Learning、Attention Mechanism、Autonom