- 当前 Bug 的表现(可附上截图)
门店的经纬度是高德的经纬度,获取到的经纬度拿到高德地图或者腾讯地图进行转换时和实际当前我的位置都是有偏差的,直线距离大概在五百米左右,通过地址搜索获取到的经纬度调用高德地图api,误差只有二十米左右
代码:
getMyLocation: function () {
/**获取位置信息 */
return new Promise((resolve, reject) => {
wx.getLocation({
success: (res) => { //
resolve(res);
this.setData({
getLocationFail: true
})
},
fail: (res) => { //拒绝授权
reject(res);
console.log('fail: ' + res);
this.setData({
getLocationFail: false
})
}
})
})
},
pipeStoreInfo: function () {
/**获取附近所有门店 */
this.getMyLocation().then((res) => {
this.setData({
getLocationFail: true
})
this.getNearStore(res.latitude, res.longitude)
}, (res) => {
this.setData({
getLocationFail: false
})
})
},
微信小程序-腾讯地图显示偏差问题
背景
小程序设置位置点
关于如何在map组件上设置位置点,详细请查看腾讯官方文档
首先看一下map组件:
<map id="map" longitude="113.324520" latitude="23.099994" scale="14" controls="{{controls}}" bindcontroltap="controltap" markers="{{markers}}" bindmarkertap="markertap" polyline="{{polyline}}" bindregionchange="regionchange" show-location style="width: 100%; height: 300px;"></map>
其markers是需要设置在地图界面的位置点数组,数组中的元素就是一个对象。
markers: [{ iconPath: "/resources/others.png", id: 0, latitude: 23.099994, longitude: 113.324520, width: 50, height: 50 }, iconPath: "/resources/others.png", id: 0, latitude: 23.094994, longitude: 113.424520, width: 50, height: 50 }],
这里的latitude和longitude如果直接给其他地图定位的点,那么相同的这个点位置,在小程序中会出现偏差;
原因
小程序中的定位方式默认的是
wgs84
,而百度地图中使用的是BD09
,还有gcj02
。所以这里会出现偏差。解决方案
使用离线的js库gcoord,将位置信息进行一次转换;
import gcoord from '../../../utils/gcoord.js'
var result = gcoord.transform( [resArr[i].JD, resArr[i].WD], // 经纬度坐标 gcoord.BD09, // 当前坐标系(百度) gcoord.GCJ02);
这里在小程序中修改为gcj02,国测数据,因此转为gcj02;
整个应该很正常,跟设备还有环境都有很大的关系
https://www.jianshu.com/p/9979f2a5ecaf 我不知道这种方式好不好用,你可以试一下,我的需求不是很大,没用过
你的转换写法呢?
转换?这还需要什么转换呢?this.getNearStore(res.latitude, res.longitude)直接去请求后端接口去了
= =你不是腾讯地图获取的经纬度么?然后直接传给高德地图的api?
对的,高德地图和腾讯地图基本偏差可以忽略不计,但是百度地图和腾讯地图、高德地图偏差较大
正常,而且还不一定能实时刷新
我就一直坐在公司里的,门店经纬度配置的就是公司地址,结果还偏差这么多,通知地址搜索偏差只有20米左右
具体原理不清楚,反正偏差几百米是有的。
我用别的小程序就正常的,不知道他们怎么处理的