本文實例為大家分享了微信小程序實現自動定位的具體代碼,供大家參考,具體內容如下
使用了騰訊地圖提供的免費api:
需要引入一個js文件:下載地址
js代碼:
// 引入SDK核心類var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');var qqmap = new QQMapWX({//在騰訊地圖開放平臺申請密鑰 http://lbs.qq.com/mykey.html key: 'your KEY'});Page({ data: { myLatitude: "", myLongitude: "", myAddress: "" }, onLoad: function(){ var that = this //用微信提供的api獲取經緯度 wx.getLocation({ type: 'wgs84', success: function(res){ that.setData({myLatitude: res.latitude, myLongitude: res.longitude}) //用騰訊地圖的api,根據經緯度獲取城市 qqmap.reverseGeocoder({ location: { latitude: that.data.myLatitude, longitude: that.data.myLongitude }, success: function (res) { console.log(res) var a = res.result.address_component //獲取市和區(區可能為空) that.setData({myAddress: a.city + a.district}) //控制臺輸出結果 console.log(that.data.myAddress) } }) } }) } })
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答