效果:我在手機上打開微信小程序,自動顯示出我當前所在的地理位置:
成都創新互聯,是成都地區的互聯網解決方案提供商,用心服務為企業提供網站建設、成都app軟件開發、微信小程序、系統按需開發網站和微信代運營服務。經過數10年的沉淀與積累,沉淀的是技術和服務,讓客戶少走彎路,踏實做事,誠實做人,用情服務,致力做一個負責任、受尊敬的企業。對客戶負責,就是對自己負責,對企業負責。
具體步驟:
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"}
新聞標題:在微信小程序里自動獲得當前手機所在的經緯度并轉換成地址
路徑分享:http://m.kartarina.com/article20/jecpco.html
成都網站建設公司_創新互聯,為您提供App設計、小程序開發、企業網站制作、關鍵詞優化、虛擬主機、營銷型網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯