日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

效果:我在手機上打開微信小程序,自動顯示出我當前所在的地理位置:

具體步驟:

1. 使用微信jssdk提供的getLocation API拿到經緯度;

2. 調用高德地圖的api使用經緯度去換取地址的文字描述。

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("無法獲取地址信息,請稍后再試");}});}});

ReservationService.getGeocode的實現:

getGeocode(location) {

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

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

}

看下面一個使用高德地圖API將經緯度轉換成文字描述的地址的例子,使用postman發送請求:

API響應:

{"status": "1","regeocode": {"addressComponent": {"city": [],"province": "北京市","adcode": "110101","district": "東城區","towncode": "110101001000","streetNumber": {"number": "44號","location": "116.39795,39.9097239","direction": "東北","distance": "117.874","street": "廣場東側路"},"country": "中國","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": "風景名勝;風景名勝相關;旅游景點"},"neighborhood": {"name": [],"type": []},"citycode": "010"},"formatted_address": "北京市東城區東華門街道天安門"},"info": "OK","infocode": "10000"}

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

轉載于:https://www.cnblogs.com/sap-jerry/p/9831508.html

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。