// pages/book/list.js var QQMapWX = require('../../res/js/qqmap-wx-jssdk.min.js'); var qqmapsdk; var App = getApp(); Page({ data: { lat:"34.911247",//可設為當前坐標 lng:"114.881576", listInfo:[//可刪除,接口獲取數據 { "name": "學校", "lat": "34.819828", "lng": "114.829617" }, { "name": "南湖公園", "lat": "34.820110", "lng": "114.818974" }, { "name": "汽車站", "lat": "34.816939", "lng": "114.813952" }, { "name": "高中", "lat": "34.817996", "lng": "114.800220" } ], info:[]//頁面渲染數據 }, onLoad: function (options) { qqmapsdk = new QQMapWX({ key: 'ED7BZ-AUWLW-IGHRD-OUWOA-DMS73-FLFWO' }); var that = this; }, onReady: function () { //獲取當前坐標 var that = this; 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; // 位置精度 // that.setData({ // lat: latitude, // lng: longitude // }); } }); }, onShow: function () { this.GetChangeJl(); }, GetChangeJl:function(){ var that = this; var listBox = that.data.listInfo; var strs = ‘’; for(var i = 0; i <listBox.length;i ++){
if (i+1 < listBox.length){
strs += listBox[i].lat + "," + listBox[i].lng + ";"
}else{
strs += listBox[i].lat + "," + listBox[i].lng
}
}; //列出所有的地圖ip,然后再函數中直接調用 qqmapsdk.calculateDistance({ from: { latitude: that.data.lat, longitude: that.data.lng }, to: strs, //strs為字符串,末尾的“;”要去掉 success: function (res) { var dataList = that.data.listInfo; var lists = res.result.elements; // console.log(lists.distance) var box = []; for (var x = 0; x < lists.length; x++) { var aa = lists[x].distance / 1000; // console.log(aa.toFixed)//保留一位小數 box[x]={ "name": listBox[x].name, "lat": listBox[x].lat, "lng": listBox[x].lng, "juli": aa.toFixed(1) } }; that.setData({ info: box }); }, fail: function (res) { // console.log(res); }, complete: function (res) { // console.log(res); } }) }, })
