• 當前位置:首頁 > IT技術 > 微信平臺 > 正文

    微信小程序獲取位置
    2021-08-05 17:11:23

    獲取位置 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獲取坐標。

    ?
    ?
    ?
    ?

    本文摘自 :https://blog.51cto.com/u

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码