本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)自動(dòng)定位的具體代碼,供大家參考,具體內(nèi)容如下
使用了騰訊地圖提供的免費(fèi)api:
需要引入一個(gè)js文件:下載地址
js代碼:
// 引入SDK核心類var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');var qqmap = new QQMapWX({//在騰訊地圖開(kāi)放平臺(tái)申請(qǐng)密鑰 http://lbs.qq.com/mykey.html key: 'your KEY'});Page({ data: { myLatitude: "", myLongitude: "", myAddress: "" }, onLoad: function(){ var that = this //用微信提供的api獲取經(jīng)緯度 wx.getLocation({ type: 'wgs84', success: function(res){ that.setData({myLatitude: res.latitude, myLongitude: res.longitude}) //用騰訊地圖的api,根據(jù)經(jīng)緯度獲取城市 qqmap.reverseGeocoder({ location: { latitude: that.data.myLatitude, longitude: that.data.myLongitude }, success: function (res) { console.log(res) var a = res.result.address_component //獲取市和區(qū)(區(qū)可能為空) that.setData({myAddress: a.city + a.district}) //控制臺(tái)輸出結(jié)果 console.log(that.data.myAddress) } }) } }) } })以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長(zhǎng)站。
新聞熱點(diǎn)
疑難解答
圖片精選