微信小程序根据经纬度坐标获取地理位置(国家城市数据)
买泛域名SSL证书 送5斤装现摘猕猴桃一箱、同时提供技开源商城搭建免费技术支持。
泛域名ssl证书 239元1年送1个月、单域名39元1年,Sectigo(原Comodo证书)全球可信证书,强大的兼容性,高度安全性,如有问题7天内可退、可开发票
加微信VX 18718058521 备注SSL证书
【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价
泛域名ssl证书 239元1年送1个月、单域名39元1年,Sectigo(原Comodo证书)全球可信证书,强大的兼容性,高度安全性,如有问题7天内可退、可开发票
加微信VX 18718058521 备注SSL证书
【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价
首先得到经纬度数据
wx.openLocation
wx.getLocation({
type: 'gcj02', //返回可以用于wx.openLocation的经纬度
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale: 18
})
}})latitude ,longitude
再调用百度或者腾讯的API,根据坐标获取地理位置,或者根据地理位置获取坐标,我们这里使用根据坐标获取地理位置
https://apis.map.qq.com/ws/geocoder/v1/?location=34.34127,108.93984&key=SMJBZ-WCHK4-ZPZUA-******
上面标红的分别为坐标经纬度数据和腾讯地图key
返回的数据结果为
{
"status": 0,
"message": "query ok",
"request_id": "fe489176-636a-11eb-8f47-525400087804",
"result": {
"location": {
"lat": 34.34127,
"lng": 108.93984
},
"address": "陕西省西安市未央区凤城八路",
"formatted_addresses": {
"recommend": "西安市政府",
"rough": "西安市政府"
},
"address_component": {
"nation": "中国",
"province": "陕西省",
"city": "西安市",
"district": "未央区",
"street": "凤城八路",
"street_number": "凤城八路"
},
"ad_info": {
"nation_code": "156",
"adcode": "610112",
"city_code": "156610100",
"name": "中国,陕西省,西安市,未央区",
"location": {
"lat": 34.25,
"lng": 108.834709
},
"nation": "中国",
"province": "陕西省",
"city": "西安市",
"district": "未央区"
},
"address_reference": {
"street_number": {
"id": "",
"title": "",
"location": {
"lat": 34.34095,
"lng": 108.961899
},
"_distance": 22.5,
"_dir_desc": "北"
},
"business_area": {
"id": "17302653615853623303",
"title": "文景路",
"location": {
"lat": 34.331902,
"lng": 108.938004
},
"_distance": 0,
"_dir_desc": "内"
},
"famous_area": {
"id": "17302653615853623303",
"title": "文景路",
"location": {
"lat": 34.331902,
"lng": 108.938004
},
"_distance": 0,
"_dir_desc": "内"
},
"crossroad": {
"id": "5083226",
"title": "凤城八路/文景路(路口)",
"location": {
"lat": 34.341011,
"lng": 108.937988
},
"_distance": 167.3,
"_dir_desc": "东"
},
"town": {
"id": "610112001",
"title": "张家堡街道",
"location": {
"lat": 34.335777,
"lng": 108.929611
},
"_distance": 0,
"_dir_desc": "内"
},
"street": {
"id": "4389039533976684679",
"title": "凤城八路",
"location": {
"lat": 34.34095,
"lng": 108.961899
},
"_distance": 22.5,
"_dir_desc": "北"
},
"landmark_l2": {
"id": "3474739998982487927",
"title": "西安市政府",
"location": {
"lat": 34.343121,
"lng": 108.939629
},
"_distance": 0,
"_dir_desc": "内"
}
}
}
}

