微信公眾號實際上是h5頁面,有些功能想要在微信中兼容性好,可以使用微信提供的jdk來實現。
微信頁面不支持直接喚醒其他的地圖app,但是可以使用openLocation間接的打開其他地圖app。
實例:從當前位置導航到你的目的地
在代碼里引入微信提供的jdk,然后config你需要的功能函數,之后才能使用這些函數
//數據全部是通過接口獲取到 wx.config({ debug: false,// 開啟調試模式,調用的所有api的返回值會在客戶端alert出來 appId: data.AppId,// 必填,公眾號的唯一標識 timestamp: data.Timestamp,// 必填,生成簽名的時間戳 nonceStr: data.NonceStr, //生成簽名的隨機串 signature: data.Signature, //簽名 jsApiList: ['openLocation']// 必填,需要使用的JS接口列表 });
wx.openLocation({ latitude: tlat,//目的地latitude longitude: tlng,//目的地longitude name: '江北機場T3航站樓', address: '重慶 江北機場T3航站樓', scale: 15//地圖縮放大小,可根據情況具體調整 });
注意:openLocation傳入的坐標必須是火星坐標
//經緯度轉換 function transformBD (lat, lng, type, callback) { var url;
var self = this; if (type == 'gcj02') { //百度坐標轉為火星坐標 url = "https://api.map.baidu.com/geoconv/v1/?coords=" + lng + "," + lat + "&from=5&to=3&ak=XXXXXXXXXX";//注意latitude 和longitude參數的順序 } else { //'bd09ll' //轉為百度坐標 url = "https://api.map.baidu.com/geoconv/v1/?coords=" + lng + "," + lat + "&from=1&to=5&ak=XXXXXXXXXXXXXX"; } $.ajax({ url: url, success: function (data, status, xhr) { if (status == 'success') { var point = data.result[0]; //y為lat,x為lng callback.call(self, point.y, point.x); } }, dataType: 'jsonp' }); }
流程:
使用openLocation打開微信自帶的騰訊地圖->選擇右下角的綠色按鈕->選擇地圖app,喚醒app會自動將你在openLocation中傳入的目的地坐標帶入