1. 程式人生 > 實用技巧 >uniapp裡H5端獲取經緯度座標的坑

uniapp裡H5端獲取經緯度座標的坑

方案一是 利用uniapp內建的 介面

使用地圖和定位相關需要在騰訊地圖開放平臺申請金鑰,填寫在manifest.json中。

https://uniapp.dcloud.io/api/location/location?id=getlocation

uni.getLocation({
    type: 'wgs84',
    success: function (res) {
        console.log('當前位置的經度:' + res.longitude);
        console.log('當前位置的緯度:' + res.latitude);
    }
});

  

方案二是 利用微信jssdk的介面

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#36

wx.getLocation({
  type: 'wgs84', // 預設為wgs84的gps座標,如果要返回直接給openLocation用的火星座標,可傳入'gcj02'
  success: function (res) {
    var latitude = res.latitude; // 緯度,浮點數,範圍為90 ~ -90
    var longitude = res.longitude; // 經度,浮點數,範圍為180 ~ -180。
var speed = res.speed; // 速度,以米/每秒計 var accuracy = res.accuracy; // 位置精度 } });