网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

在微信小程序里自动获得当前手机所在的经纬度并转换成地址

效果:我在手机上打开微信小程序,自动显示出我当前所在的地理位置:

成都创新互联,是成都地区的互联网解决方案提供商,用心服务为企业提供网站建设、成都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://bjjierui.cn/article/jecpco.html

其他资讯