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) } })