微信小程序获取当前城市定位
生活随笔
收集整理的這篇文章主要介紹了
微信小程序获取当前城市定位
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
獲取當(dāng)前城市 一般需要二步驟
1?getLocation 獲取精度,維度
2 然后請求,百度,高度,騰訊題圖轉(zhuǎn)換即可
這里以百度地圖為例
代碼如下
wx.getLocation({success: function(res) {console.log(res)var longitude = res.longitudevar latitude = res.latitudethat.curCity(longitude, latitude)},})},curCity: function (longitude, latitude){wx.request({url: 'https://api.map.baidu.com/geocoder/v2/?ak=自己到百度地圖創(chuàng)建獲取的ak&location=' + latitude + ',' + longitude + '&output=json&pois=1',data:{},header: {'Content-Type': 'application/json'},success:function(res){console.log(res);var city = res.data.result.addressComponent.cityconsole.log("定位的城市"+city)}})},
這個就能得到當(dāng)前所在位置的城市了
注意:ak要到百度地圖開放平臺申請即可,如果填寫不能會報(bào)錯,?getLocation使用的使用記得添加權(quán)限,如果不添加不提示添加的
總結(jié)
以上是生活随笔為你收集整理的微信小程序获取当前城市定位的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 烟台光子嫩肤一次多少钱?
- 下一篇: 粟开头的成语有哪些?