最近做微信小程序項(xiàng)目中遇到根據(jù)后臺(tái)接口獲取城市某個(gè)區(qū)域內(nèi)的信息,后臺(tái)接口要求傳入城市的區(qū)域名稱弥奸,例如上海市楊浦區(qū)蛤克,小程序官方地址提供的API只能獲取到用戶當(dāng)前的經(jīng)緯度,如何通過經(jīng)緯度查詢到用戶的當(dāng)前位置成了一個(gè)問題闷祥。所以通過研究和查詢資料解決了這個(gè)問題娱颊,現(xiàn)共享給大家。
我是通過騰訊地圖逆地址解析凯砍,在通過經(jīng)緯度獲取詳細(xì)的位置信息數(shù)據(jù)箱硕。
根據(jù)騰訊地圖API,以圖文的方式說明如何獲取詳細(xì)的位置信息數(shù)據(jù)果覆。具體參考騰訊地圖Webservice API的介紹颅痊。地址:騰訊位置服務(wù)
step1:申請騰訊地圖密鑰(key),申請地址:申請密鑰
填寫完成后即可獲取到對應(yīng)的key值局待。
申請騰訊地圖密鑰.png
step2:通過小程序官方API獲取用戶當(dāng)前位置經(jīng)緯度斑响。然后根據(jù)騰訊Webservice API逆地址解析相關(guān)介紹,傳入獲取到的經(jīng)緯度钳榨,即可獲取舰罚。
示例代碼:
//獲取當(dāng)前位置經(jīng)緯度
wx.getLocation({
type: 'wgs84',
success: function (res) {
//console.log("獲取當(dāng)前經(jīng)緯度:" + JSON.stringify(res));
//發(fā)送請求通過經(jīng)緯度反查地址信息
var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + res.latitude + "," + res.longitude + "&key=你的key值&get_poi=1";
common.Request(getAddressUrl, "get", "", function (ops) {
//console.log(JSON.stringify(ops));
})
}
})
獲取當(dāng)前經(jīng)緯度.jpg
獲取的位置示例,根據(jù)項(xiàng)目提取需要的數(shù)據(jù)薛耻。
{
"status": 0,
"message": "query ok",
"request_id": "7e11ac8e-f763-11e7-b568-6c92bf3a15eb",
"result": {
"location": {
"lat": 39.984154,
"lng": 116.30749
},
"address": "北京市海淀區(qū)北四環(huán)西路66號(hào)",
"formatted_addresses": {
"recommend": "海淀區(qū)中國技術(shù)交易大廈(左岸工社東)",
"rough": "海淀區(qū)中國技術(shù)交易大廈(左岸工社東)"
},
"address_component": {
"nation": "中國",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)",
"street": "北四環(huán)西路",
"street_number": "北四環(huán)西路66號(hào)"
},
"ad_info": {
"nation_code": "156",
"adcode": "110108",
"city_code": "156110000",
"name": "中國,北京市,北京市,海淀區(qū)",
"location": {
"lat": 39.984154,
"lng": 116.307487
},
"nation": "中國",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"address_reference": {
"business_area": {
"title": "中關(guān)村",
"location": {
"lat": 39.984089,
"lng": 116.307564
},
"_distance": 0,
"_dir_desc": "內(nèi)"
},
"famous_area": {
"title": "中關(guān)村",
"location": {
"lat": 39.984089,
"lng": 116.307564
},
"_distance": 0,
"_dir_desc": "內(nèi)"
},
"crossroad": {
"title": "彩和坊路/北四環(huán)西路輔路(路口)",
"location": {
"lat": 39.985001,
"lng": 116.308113
},
"_distance": 102.8,
"_dir_desc": "西南"
},
"town": {
"title": "海淀街道",
"location": {
"lat": 39.984154,
"lng": 116.307487
},
"_distance": 0,
"_dir_desc": "內(nèi)"
},
"street_number": {
"title": "北四環(huán)西路66號(hào)",
"location": {
"lat": 39.984119,
"lng": 116.307503
},
"_distance": 6.2,
"_dir_desc": ""
},
"street": {
"title": "彩和坊路",
"location": {
"lat": 39.984169,
"lng": 116.308098
},
"_distance": 46.6,
"_dir_desc": "西"
},
"landmark_l1": {
"title": "左岸工社",
"location": {
"lat": 39.984112,
"lng": 116.30439
},
"_distance": 176,
"_dir_desc": "東"
},
"landmark_l2": {
"title": "中國技術(shù)交易大廈A座",
"location": {
"lat": 39.984329,
"lng": 116.307419
},
"_distance": 20.4,
"_dir_desc": ""
}
},
"poi_count": 10,
"pois": [
{
"id": "2845372667492951071",
"title": "中國技術(shù)交易大廈A座",
"address": "北京市海淀區(qū)北四環(huán)西路66號(hào)",
"category": "房產(chǎn)小區(qū):商務(wù)樓宇",
"location": {
"lat": 39.984329,
"lng": 116.307419
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 20.4,
"_dir_desc": ""
},
{
"id": "11939717548889564206",
"title": "中國技術(shù)交易大廈-西門",
"address": "北京市海淀區(qū)北四環(huán)西路66號(hào)附近",
"category": "室內(nèi)及附屬設(shè)施:通行設(shè)施類:門/出入口",
"location": {
"lat": 39.98415,
"lng": 116.307281
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 17.6,
"_dir_desc": ""
},
{
"id": "13097787876388519900",
"title": "中國技術(shù)交易大廈-東門",
"address": "北京市海淀區(qū)彩和坊路與海淀北一街交叉口西北50米",
"category": "室內(nèi)及附屬設(shè)施:通行設(shè)施類:門/出入口",
"location": {
"lat": 39.984131,
"lng": 116.307716
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 19.7,
"_dir_desc": ""
},
{
"id": "12925244666643621769",
"title": "中國技術(shù)交易大廈B座",
"address": "北京市海淀區(qū)北四環(huán)西路66",
"category": "房產(chǎn)小區(qū):商務(wù)樓宇",
"location": {
"lat": 39.984112,
"lng": 116.307587
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 9.7,
"_dir_desc": ""
},
{
"id": "3629720141162880123",
"title": "中國技術(shù)交易大廈",
"address": "北京市海淀區(qū)北四環(huán)西路66號(hào)",
"category": "房產(chǎn)小區(qū):商務(wù)樓宇",
"location": {
"lat": 39.984089,
"lng": 116.307564
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 0,
"_dir_desc": "內(nèi)"
},
{
"id": "9969038414753335812",
"title": "騰訊科技(北京)有限公司(中國技術(shù)交易大廈)",
"address": "北京市海淀區(qū)北四環(huán)西路66號(hào)中國技術(shù)交易大廈",
"category": "公司企業(yè):公司企業(yè)",
"location": {
"lat": 39.984131,
"lng": 116.307503
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 2.9,
"_dir_desc": ""
},
{
"id": "12689244359326172642",
"title": "車庫咖啡",
"address": "北京市海淀區(qū)中關(guān)村創(chuàng)業(yè)大街6號(hào)樓2層",
"category": "娛樂休閑:咖啡廳",
"location": {
"lat": 39.983898,
"lng": 116.306908
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 57.1,
"_dir_desc": "東北"
},
{
"id": "3187032738687555052",
"title": "中關(guān)村創(chuàng)業(yè)大街",
"address": "北京市海淀區(qū)海淀西大街",
"category": "購物:商業(yè)步行街",
"location": {
"lat": 39.984741,
"lng": 116.306519
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 43.9,
"_dir_desc": "東北"
},
{
"id": "7246616758286733108",
"title": "基督教堂(彩和坊路)",
"address": "北京市海淀區(qū)彩和坊路9號(hào)",
"category": "旅游景點(diǎn):教堂",
"location": {
"lat": 39.983269,
"lng": 116.307648
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 69.5,
"_dir_desc": "北"
},
{
"id": "14510474916445262010",
"title": "言幾又(中關(guān)村店)",
"address": "北京市海淀區(qū)海淀西大街48號(hào)",
"category": "購物:圖書音像",
"location": {
"lat": 39.98373,
"lng": 116.30661
},
"ad_info": {
"adcode": "110108",
"province": "北京市",
"city": "北京市",
"district": "海淀區(qū)"
},
"_distance": 88.4,
"_dir_desc": "東北"
}
]
}
}
在小程序里使用注意的地方:
- 需要在微信小程序后臺(tái)配置合法域名营罢;
- 測試時(shí)也可以在開發(fā)工具中選擇不校驗(yàn)安全域名。
其它文章請?jiān)L問: