这也是一个新手容易犯的错误,技术文档中可以找到wx.getlocation得到的默认坐标系是wgs84即GPS坐标系,但是在chooselocation和openlocation中并未再次提及坐标系type,其实在wx.getlocation中提及过默认返回wgs84,但是可以通过制定type为gcj02用于openlocation,同样在wx.openlocation中的示例也用到了这点:
其实只有wx.gentlocation默认返回的是gps系,而其他方法都使用的是火星坐标系也就是gcj02,包括腾讯地图在内,这就是新手开发过程中getlocation不写type,或者是type: 'wgs84',这样就会引起地图坐标总是不对的原因。
真不明白为什么偏偏只有getlocation默认gps坐标,而其他地方包括腾讯地图API在内用的却是火星坐标。
wx.getlocation文档
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
type | string | wgs84 | 否 | wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 |
wx.openlocation示例
wx.getLocation({
type: 'gcj02', //返回可以用于wx.openLocation的经纬度
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale: 18
})
}
})