效果:我在手機(jī)上打開微信小程序,自動(dòng)顯示出我當(dāng)前所在的地理位置:
成都創(chuàng)新互聯(lián),是成都地區(qū)的互聯(lián)網(wǎng)解決方案提供商,用心服務(wù)為企業(yè)提供網(wǎng)站建設(shè)、成都app軟件開發(fā)、微信小程序、系統(tǒng)按需開發(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": "東華門街道","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"}
新聞標(biāo)題:在微信小程序里自動(dòng)獲得當(dāng)前手機(jī)所在的經(jīng)緯度并轉(zhuǎn)換成地址
路徑分享:http://www.kartarina.com/article20/jecpco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、小程序開發(fā)、企業(yè)網(wǎng)站制作、關(guān)鍵詞優(yōu)化、虛擬主機(jī)、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)