微信腾讯地图计算两点间距离

// 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 ++){
        strs[i] = {
          latitude: listBox[i].lat,
          longitude: listBox[i].lng 
        };
    };
    //列出所有的地图ip,然后再函数中直接调用 
    qqmapsdk.calculateDistance({
      from: {
        latitude: that.data.lat,
        longitude: that.data.lng
      },
      to: 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);
      }
    })
  },
})    

猜你喜欢

转载自www.cnblogs.com/hermitks/p/9350477.html
今日推荐