微信小程序地图之逆地理编码
首先说一下,我微信自带map的api中并没有相关接口可调用。文中的方法建立于高德地图。(顺便吐槽,微信开发文档相比支付宝家的显得好烂!)
最近做项目用到地图定位相关的需求,为了搞定需求看了下相关的文档。最后选择高德地图小程序api,腾讯、百度地图应该也可以并且相似但是我没有深入研究。
首先看到的是逆地理编码的api,看到注释不免小小的兴奋了一下。
但是看到了api,瞬间感觉不和谐,指定位置在哪呢?怎么传入经纬度?
最后研究高德插件的js文件的方法,提取出了相关方法:
AMapWX.prototype.getRegeo = function(a) {
function c(c) {
var d = b.requestConfig;
wx.request({
url: "https://restapi.amap.com/v3/geocode/regeo",
data: {
key: b.key,
location: c,
extensions: "all",
s: d.s,
platform: d.platform,
appname: b.key,
sdkversion: d.sdkversion,
logversion: d.logversion
},
method: "GET",
header: {
"content-type": "application/json"
},
success: function(b) {
var d, e, f, g, h, i, j, k, l;
b.data.status && "1" == b.data.status ? (d = b.data.regeocode, e = d.addressComponent, f = [], g = "", d && d.roads[0] && d.roads[0].name && (g = d.roads[0].name + "附近"), h = c.split(",")[0], i = c.split(",")[1], d.pois && d.pois[0] && (g = d.pois[0].name + "附近", j = d.pois[0].location, j && (h = parseFloat(j.split(",")[0]), i = parseFloat(j.split(",")[1]))), e.provice && f.push(e.provice), e.city && f.push(e.city), e.district && f.push(e.district), e.streetNumber && e.streetNumber.street && e.streetNumber.number ? (f.push(e.streetNumber.street), f.push(e.streetNumber.number)) : (k = "", d && d.roads[0] && d.roads[0].name && (k = d.roads[0].name), f.push(k)), f = f.join(""), l = [{
iconPath: a.iconPath,
width: a.iconWidth,
height: a.iconHeight,
name: f,
desc: g,
longitude: h,
latitude: i,
id: 0,
regeocodeData: d
}], a.success(l)) : a.fail({
errCode: b.data.infocode,
errMsg: b.data.info
})
},
fail: function(b) {
a.fail({
errCode: "0",
errMsg: b.errMsg || ""
})
}
})
}
var b = this;
a.location ? c(a.location) : b.getWxLocation(a, function(a) {
c(a)
})
}
提取出的方法如下
wx.request({
url: 'https://restapi.amap.com/v3/geocode/regeo',
data: {
key: key,
location: that.data.longitude + "," + that.data.latitude ,
extensions:"all",
s:"rsx",
sdkversion:"sdkversion",
logversion:"logversion"
},
success: function (res) {
that.setData({
textData: {
name: res.data.regeocode.pois[0].name,
desc: res.data.regeocode.formatted_address
}
})
console.log(JSON.stringify(res.data.regeocode.formatted_address));
},
fail: function (res) {
}
})
友情链接:
技术博客 简书主页