日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

在微信小程序里自动获得当前手机所在的经纬度并转换成地址

發(fā)布時(shí)間:2025/5/22 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在微信小程序里自动获得当前手机所在的经纬度并转换成地址 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

效果:我在手機(jī)上打開微信小程序,自動(dòng)顯示出我當(dāng)前所在的地理位置:

具體步驟:

1. 使用微信jssdk提供的getLocation API拿到經(jīng)緯度;

2. 調(diào)用高德地圖的api使用經(jīng)緯度去換取地址的文字描述。

wx.ready(() => {wx.getLocation({type: "gcj02",success: function(res) {var location ="&location=" + res.longitude + "," + res.latitude;ReservationService.getGeocode(location).then(response => {if (response.data.status === "1" &&response.data.info === "OK") {self.country =response.data.regeocode.addressComponent.country;self.city = response.data.regeocode.addressComponent.city;var express ="/" +self.country +"|" +response.data.regeocode.addressComponent.province +"|" +self.city +"|" +response.data.regeocode.addressComponent.district +"|" +response.data.regeocode.addressComponent.township +"/g";self.address = response.data.regeocode.formatted_address.replace(eval(express),"");} else {// get location error, plz input addressalert("無(wú)法獲取地址信息,請(qǐng)稍后再試");}});}});

ReservationService.getGeocode的實(shí)現(xiàn):

getGeocode(location) {

const key = '此處填入您高德地圖app的key';

return axios.post('https://restapi.amap.com/v3/geocode/regeo?key=' + key + location);

}

看下面一個(gè)使用高德地圖API將經(jīng)緯度轉(zhuǎn)換成文字描述的地址的例子,使用postman發(fā)送請(qǐng)求:

API響應(yīng):

{"status": "1","regeocode": {"addressComponent": {"city": [],"province": "北京市","adcode": "110101","district": "東城區(qū)","towncode": "110101001000","streetNumber": {"number": "44號(hào)","location": "116.39795,39.9097239","direction": "東北","distance": "117.874","street": "廣場(chǎng)東側(cè)路"},"country": "中國(guó)","township": "東華門街道","businessAreas": [{"location": "116.3998109423077,39.90717459615385","name": "天安門","id": "110101"},{"location": "116.39981058278138,39.92383706953642","name": "景山","id": "110101"},{"location": "116.4118112683418,39.91461494422115","name": "王府井","id": "110101"}],"building": {"name": "天安門","type": "風(fēng)景名勝;風(fēng)景名勝相關(guān);旅游景點(diǎn)"},"neighborhood": {"name": [],"type": []},"citycode": "010"},"formatted_address": "北京市東城區(qū)東華門街道天安門"},"info": "OK","infocode": "10000"}

要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙":

轉(zhuǎn)載于:https://www.cnblogs.com/sap-jerry/p/9831508.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的在微信小程序里自动获得当前手机所在的经纬度并转换成地址的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。