效果:我在手機(jī)上打開(kāi)微信小程序,自動(dòng)顯示出我當(dāng)前所在的地理位置:
成都創(chuàng)新互聯(lián),是成都地區(qū)的互聯(lián)網(wǎng)解決方案提供商,用心服務(wù)為企業(yè)提供網(wǎng)站建設(shè)、成都app軟件開(kāi)發(fā)、微信小程序、系統(tǒng)按需開(kāi)發(fā)網(wǎng)站和微信代運(yùn)營(yíng)服務(wù)。經(jīng)過(guò)數(shù)10年的沉淀與積累,沉淀的是技術(shù)和服務(wù),讓客戶少走彎路,踏實(shí)做事,誠(chéng)實(shí)做人,用情服務(wù),致力做一個(gè)負(fù)責(zé)任、受尊敬的企業(yè)。對(duì)客戶負(fù)責(zé),就是對(duì)自己負(fù)責(zé),對(duì)企業(yè)負(fù)責(zé)。
具體步驟:
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": "東華門(mén)街道","businessAreas": [ {"location": "116.3998109423077,39.90717459615385","name": "天安門(mén)","id": "110101"}, {"location": "116.39981058278138,39.92383706953642","name": "景山","id": "110101"}, {"location": "116.4118112683418,39.91461494422115","name": "王府井","id": "110101"} ],"building": {"name": "天安門(mén)","type": "風(fēng)景名勝;風(fēng)景名勝相關(guān);旅游景點(diǎn)"},"neighborhood": {"name": [],"type": [] },"citycode": "010"},"formatted_address": "北京市東城區(qū)東華門(mén)街道天安門(mén)"},"info": "OK","infocode": "10000"}