在小程序中獲取地理位置信息的流程就是:
1.根據wx.getLocation方法獲取當前位置坐標。
2.根據reverseGeocoder方法獲取當前坐標地理位置信息。
具體實現如下:
//1.1引入sdk核心類(寫在app.js中,其他頁面用到直接引用)
var QQMapWX = require('/utils/qqmap-wx-jssdk.min.js'); //app.js App({ onLaunch: function () { var that = this;
//1.2實例化騰訊地圖API核心類 that.globalData.qqmapsdk = new QQMapWX({ key: '開發秘鑰' }); //1.3wx.getLocation方法獲取當前位置坐標。 wx.getLocation({ altitude:false, success: function (res) { var latitude = res.latitude; var longitude = res.longitude; that.globalData.location={ latitude: latitude, longitude: longitude } } }); }, globalData: { url: '' } })
說明:根據wx.getLocation方法獲取當前位置坐標。這個是寫在app.js中,某個頁面用的時候在某頁面的js文件中根據qqmapsdk.reverseGeocoder方法獲取當前坐標地理位置信息。
onLoad: function (options) { var that = this; app.globalData.qqmapsdk.reverseGeocoder({ //qqmapsdk.reverseGeocoder location: { latitude: app.globalData.location.latitude, longitude: app.globalData.location.longitude }, success: function (res) { var address = res.result.address; that.setData({ current_address: address }); }, fail: function (res) { wx.showToast({ title: '解析地址錯誤', icon: 'loading', duration: 1000 }); }, }) }