微信小程序获取位置

获取位置 getLocation

wx.getLocation({ type: 'wgs84', success (res) { const latitude = res.latitude const longitude = res.longitude const speed = res.speed const accuracy = res.accuracy }})

wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标

打开地图

wx.getLocation({ type: 'gcj02', //返回可以用于wx.openLocation的经纬度 success (res) { const latitude = res.latitude const longitude = res.longitude wx.openLocation({ latitude, longitude, scale: 18 }) }})

wgs84是全球定位系统,获取的坐标,gcj02是国家测绘局给出的坐标。
gcj02火星坐标系,国测局02年发布的坐标体系,它是一种对经纬度数据的加密算法,即加入随机的偏差。高德、腾讯、Google中国地图使用。国内最广泛使用的坐标体系。
高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系。
百度地图使用的是BD-09坐标系。
底层接口(HTML5 Geolocation或ios、安卓API)通过GPS设备获取的坐标使用的是WGS-84坐标系。

经度0°——180°(东行,标注E)0°——180°(西行,标注W) 纬度0°——90°N、0°——90°S。
润园北门
腾讯地图坐标,118.284618,33.920469。(LNG,LAT)
高德地图坐标,118.284614,33.920445。(LNG,LAT)
百度地图坐标,118.291152,33.926284。(LNG,LAT)

在线转换,http://www.gpsspg.com/maps.htm

技术图片

经纬度转化,百度转腾讯高德。

/** * 中国正常GCJ02坐标---->百度地图BD09坐标 * 腾讯地图用的也是GCJ02坐标 * @param double $lng 经度 * @param double $lat 纬度 * @return array */public static function Convert_GCJ02_To_BD09($lng, $lat){ $x_pi = 3.14159265358979324 * 3000.0 / 180.0; $x = $lng; $y = $lat; $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi); $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi); $lng = $z * cos($theta) + 0.0065; $lat = $z * sin($theta) + 0.006; return array('lng' => $lng, 'lat' => $lat);}/** * 百度地图BD09坐标---->中国正常GCJ02坐标 * 腾讯地图用的也是GCJ02坐标 * @param double $lng 经度 * @param double $lat 纬度 * @return array */public static function Convert_BD09_To_GCJ02($lng, $lat){ $x_pi = 3.14159265358979324 * 3000.0 / 180.0; $x = $lng - 0.0065; $y = $lat - 0.006; $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi); $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi); $lng = $z * cos($theta); $lat = $z * sin($theta); return array('lng' => $lng, 'lat' => $lat);}

gcj02

'lng' => '118.34593200683594''lat' => '33.9527587890625'

wgs84

'lng' => '118.34032440185547''lat' => '33.95400619506836'

实验证明,如果想比对腾讯地图坐标距离,请用gcj02获取坐标。

相关文章