高德,微信公眾號,企業微信獲取定位


微信公眾號開發文檔:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

高德文檔:https://lbs.amap.com/api/javascript-api/reference-amap-ui/other/positionpicker

1.高德地圖: 

1-1.獲取當前定位信息

 AMap.plugin(['AMap.Geolocation'],  ()=> {     
        const geolocation =  new AMap.Geolocation({timeout: 10000});
        map.addControl(geolocation);
        geolocation.getCurrentPosition( (status, result) =>{
            if (status == 'complete') {
                onComplete(result)
            } else {
                onError(result)
            }
        });
    })

1-2. 根據經緯度獲取定位信息

function getWGSLocation(locationarg){ // 傳入經緯度,格式:`${longitude},${latitude}`
    return new Promise(function(resolve, reject){
        AMap.convertFrom(locationarg,
          "gps",
          (status, result) => { // WGS84 轉高德
            if (status.toLowerCase() === 'complete' && result.info.toLowerCase() === 'ok') {
              const lon = result.locations[0].getLng();
              const lat = result.locations[0].getLat();
              geocoder.getAddress([lon, lat],
                (s, r) => {
                  if (s.toLowerCase() === 'complete' && r.info.toLowerCase() === 'ok') {
                    resolve(r)
                  } else {
                    console.log('error');                    
                  }
                });
            } else {
              console.log('error');
            }
          });

    })
}

2.微信公眾號獲取定位:

2-1.配置wx.config(為了安全,所有信息要從服務端獲取)

import wx from 'weixin-jsapi'; //npm安裝weixin-jsapi 或者直接頁面引用

// 通過后端的接口拿到appid,簽名...等信息,注:當前頁面的href不可以有#,否去取不到
axios.post('/Home/GetWxJsSdkParam', {url:location.href.split('#')[0]}).then((response) => {
    const c = res;
    wx.config({
        beta: true,// 必須這么寫,否則wx.invoke調用形式的jsapi會有問題
        // debug: true, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。
        appId: c.AppId, // 必填,企業號的唯一標識,此處填寫企業號corpid
        timestamp: c.TimeStamp, // 必填,生成簽名的時間戳
        nonceStr: c.NonceStr, // 必填,生成簽名的隨機串
        signature: c.Sign,// 必填,簽名,見附錄1
        jsApiList: ['getLocation'] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
    });
})

2-2.獲取當前位置的經緯度,然后根據微信返回的經緯度,調用上面高德地圖的1-2的getWGSLocation方法

 const that = this;
      wx.getLocation({
        type: 'wgs84', // 默認為wgs84的gps坐標,如果要返回直接給openLocation用的火星坐標,可傳入'gcj02'
        success: function (res) {
          console.log(res)
          const latitude = res.latitude; // 緯度,浮點數,范圍為90 ~ -90
          const longitude = res.longitude; // 經度,浮點數,范圍為180 ~ -180。 

          // `${longitude},${latitude}`
          getWGSLocation(longitude+','+latitude).then((r)=>{
              console.log(r);// 獲取的具體位置信息;
          })
        },
        fail:function(err){
          console.log('微信調用失敗哦');
          console.log(err); 
        }
      });

3.企業微信(同微信公眾號)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM