高德地图API实现定位、位置搜索、周边搜索(H5-Vue-微信小程序)
高德开放平台注册账号,申请Key高德MapsJSAPI2.0DocumentationJSAPI2.0版本提供两种导入JSAPI的方式:JSAPILoader(推荐)直接导入JSAPI脚本这里是JSAPILoader方法(可以有效避免异步加载问题,不会在执行多次加载操作时重复请求网络资源等.)。1.普通html页面在页面
复制代码获取位置//获取位置函数getLocation(){letgeolocation=newAMap.Geolocation({enableHighAccuracy:true,//是否获取高精度定位,可能影响效率,默认falsetimeout:10000,//定位超时,msneedAddress:true,//是否对定位进行逆地理编码resultsextensions:'all',//是否需要详细的地理编码信息,默认为'base'})//获取精确位置geolocation.getCurrentPosition(function(status,result){console.log(status);console.log(result);})//获取城市信息geolocation.getCityInfo(function(status,result){console.log(status);console.log(result);})}复制代码placesearch//地址搜索函数placeSearch(搜索){如果(!搜索)返回;letplaceSearch=newAMap.PlaceSearch({city:'沧州',//兴趣点城市,支持城市名,citycode,adcodecitylimit:true,//是否必须在设置的城市内搜索,默认false//type:'',//POI类别,以'|'分隔,默认:'餐饮服务|商业住宅|生活服务'pageSize:20,//每页条数,默认10,范围1-50pageIndex:1,//页码extensions:'all',//默认base,返回基本地址信息;all:返回详细信息//map:map,//map实例,可选//panel:'panel',//结果列表的id容器,optionalautoFitView:true,//是否自动调整地图视图到标记点在可见范围内})placeSearch.search(search,function(status,result){console.log(result)})}复制代码Nearbysearch//NearbysearchfunctionsearchNear(){letplaceSearch=newAMap.PlaceSearch({//city:'',//POIcitycitylimit:true,//是否在设置的城市强制搜索,默认false//type:'',//POI类别,以'|'分隔,默认:'餐饮服务|商业住宅|生活服务'pageSize:20,//每页条目数,默认10条,范围1-50pageIndex:1,//页码extensions:'all',//默认base,返回基本地址信息;all:返回详细信息//map:map,//地图实例,可选//panel:'panel',//结果列表的id容器,可选//autoFitView:true,//是否自动调整地图视图到标记点在可见范围内})letkeywords='',//关键字位置=[116.857461,38.310582],//中心点经纬度radius=2000;//搜索半径0-50000placeSearch.searchNearBy(keywords,position,radius,function(status,result){console.log(result)})}复制代码2.Vueinstallnpmi@amap/amap-jsapi-loader--saveuse复制代码3.微信小程序(小程序的key和web端的不同,需要重新制作)文档SDK下载地址constamapFile=require('../../common/amap-wx.130.js')import{AMapKey_WX}从'../。./config.js'Page({data:{latitude:'',longitude:''},onLoad:function(){this.initAMap()},initAMap(){constthat=thiswx.getLocation({成功:function(res){that.setData({latitude:res.latitude,longitude:res.longitude},function(){that.loadCity()})}})},//获取位置信息loadCity(){constAMapWx=newamapFile.AMapWX({key:AMapKey_WX})constthat=thislet{longitude,laltitude}=this.dataAMapWx.getRegeo({extensions:'base',location:`${longitude},${latitude}`,success:function(res){console.log(res)//返回结果包含省,city,district,地址描述(例如:沧州工业和信息化局附近),以及附近的POI},fail:function(res){console.log(res)}})},//placesearchplaceSearch(search){constAMapWx=newamapFile.AMapWX({key:AMapKey_WX})constthat=thisAMapWx.getInputtips({keywords:search,//查询关键字city:'沧州市',//感兴趣的城市citylimit:true,//是否限制在当前城市内搜索pageSize:40,//单页显示的结果数pageIndex:1,//页码//location:'',//经纬度成功用逗号分隔的字符串:function(data){if(data&&data.tips){that.setData({poisList:data.tips})}}})},//搜索周边POIgetPOIAround(search){constAMapWx=newamapFile.AMapWX({key:AMapKey_WX})constthat=thislet{经度,纬度}=this.dataAMapWx.getPoiAround({querykeywords:search,//搜索关键字location:`${longitude},${latitude}`,success:function(res){console.log('检索成功')console.log(res)that.poisList=res.poisData},失败:function(res){console.log(res)}})}})