wx.getLocation({ type: "wgs84", //wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 gcj02在android机上有bug,没法选择位置 success(res) { } });
wx.chooseLocation({ success(res) { let name = res.name; //名称 let address = res.address; //详细地址 let longitude = res.longitude;//经度 let latitude = res.latitude;//纬度 fail: function(info){ //失败回调 console.log(info) } }) } });
openLocation(item){ let longitude = item.longitude; let latitude = item.latitude; wx.openLocation({ latitude, longitude, scale: 18 }); },
微信小程序位置api并无提供获取省份城市的信息,这里使用高德第三方地图来获取省份城市vue
将https://restapi.amap.com添加到微信小程序合法域名里面android
下载高德微信小程序sdk
https://lbs.amap.com/api/wx/downloadgit
高德微信小程序sdk文档说明
https://lbs.amap.com/api/wx/reference/coreweb
import amapFile from "../../../../../static/sdk/amap-wx";
mounted() { this.initMap(); }, initMap(){ this.myAmapFun = new amapFile.AMapWX({key:'app key'}); }, that.myAmapFun.getRegeo({ location:`${longitude},${latitude}`, success: function(data){ let province = data[0].regeocodeData.addressComponent.province; let city = data[0].regeocodeData.addressComponent.city; // city:[] if(Object.prototype.toString.call(city)=="[object Array]"){ city = city.join(''); } that.province = province; that.city = city; }, fail: function(info){ //失败回调 console.log(info) } })
返回参数说明
https://lbs.amap.com/api/webservice/guide/api/georegeo/#regeo小程序
import amapFile from'../static/sdk/amap-wx'; //将其绑定到vue原型上 //使用 this.$myAmapFun访问 let myAmapFun = new amapFile.AMapWX({ key: 'app key' }); Vue.prototype.$myAmapFun = myAmapFun
that.$myAmapFun.getRegeo({ location:`${longitude},${latitude}`, success: function(data){ let province = data[0].regeocodeData.addressComponent.province; let city = data[0].regeocodeData.addressComponent.city; // city:[] if(Object.prototype.toString.call(city)=="[object Array]"){ city = city.join(''); } that.province = province; that.city = city; }, fail: function(info){ //失败回调 console.log(info) } })