mapapi.js 4.77 KB
// 引入SDK核心类
const QQMapWX = require('../utils/qqmap-wx-jssdk.min.js');
var app = getApp();
const key = app.globalData.smartCarMapKey;
// 实例化API核心类
const myMap = new QQMapWX({
  key: key // 必填
});

/**
 * 两点路线规划
 */
function routePlanning(flng, flat, tlng, tlat, succ, mode = 'driving'){
  // console.log(flng + "," + flat + "," + tlng + "," + tlat)
  var _this = this;
  //网络请求设置
  // var url = 'https://apis.map.qq.com/ws/direction/v1/driving/?from='+flat+ ',' + flng +'&to='+ tlat + ',' + tlng +'&key=' + key;
  var url = `https://apis.map.qq.com/ws/direction/v1/${mode}/?from=${flat},${flng}&to=${tlat},${tlng}&key=${key}`

  // console.log(url);
  var opt = {
    url: url,
    method: 'GET',
    dataType: 'json',
    success: function(res){
      console.log(res);
      var ret = res.data;
      if (succ && typeof succ === 'function') {
        if (ret.status != 0) {
          // 服务异常
          return;
        }
        succ(ret.result.routes[0]);
      }
    }
  }
  wx.request(opt);
}

// 
function direction(mode, flng, flat, tlng, tlat, succ) {
  // var url = 'https://apis.map.qq.com/uri/v1/routeplan?from='+flat+ ',' + flng +'&to='+ tlat + ',' + tlng +'&key=' + key;
  var url = `https://apis.map.qq.com/uri/v1/routeplan?type=${mode}&fromcoord=${flat},${flng}&tocoord=${tlat},${tlng}&key=${key}`
  console.log(url)
  var opt = {
    url: url,
    method: 'GET',
    dataType: 'json',
    success: function (res) {
      console.log(res)
      var ret = res.data;
      if (succ && typeof succ === 'function') {
        if (ret.status != 0) {
          // 服务异常
          return;
        }
        succ(ret.result.routes[0]);
      }
    },
  }
  wx.request(opt)
  // myMap.direction({
  //   mode: mode,
  //   from:{
  //     latitude: flat,
  //     longitude: flng
  //   },
  //   to: {
  //     latitude: tlat,
  //     longitude: tlng
  //   },
  //   success: function (res) {
  //     if (cb && typeof cb === 'function') {
  //       console.log(res)
  //       cb(res);
  //     }
  //   },
  //   fail: function (res) {
  //     console.log(res);
  //   }
  // })
}

//两点之间的距离
function calculateDistance(flng, flat, tlng, tlat,cb){
  // console.log(flng + ","+ flat+","+ tlng+","+ tlat);
  //默认计算步行距离
  myMap.calculateDistance({
    from:{
      latitude: flat,
      longitude: flng
    },
    to: [{
      latitude: tlat,
      longitude: tlng
    }],
    success: function (res) {
      if(res.status == "0"){
        if (cb && typeof cb === 'function') {
          console.log(res)
          cb(res.result.elements[0].distance);
        }
      }
    },
    fail: function (res) {
      console.log(res);
    }
  });
}
// 
// function reverseAddresss() {

// }
//逆地址解析
function detailedAdrress(tlng, tlat, da, fa, type){
  if (tlng && tlat){
    var coord_type = 5;
    if (null != type) {
      coord_type = type
    }
    myMap.reverseGeocoder({
      location: {
        latitude: tlat,
        longitude: tlng
      },
      coord_type: coord_type,
      success: function (res) {
        // console.log(res);
        if (res.status == "0") {
          if (da && typeof da === 'function') {
            da(res.result);
          }
        }
      },
      fail: function (res) {
        console.log(res);
        if (fa && typeof fa === 'function') {
          fa();
        }
      }
    });
  }
}
/**地址解析 */
const getCoord = (address,callback) =>{
  myMap.geocoder({
    address:address,
    success(res){
      callback(res)
    }
  })
}
function keywordSearch(keyword,loca,iconPath,callback){
  // 调用接口
  myMap.search({
    keyword: keyword,  //搜索关键词
    location: loca,  //设置周边搜索中心点
    page_size: 20,
    success: function (res) { //搜索成功后的回调
    console.log(res);
      var mks = []
      for (var i = 0; i < res.data.length; i++) {
        var obj = res.data[i];
        mks.push({ // 获取返回结果,放到mks数组中
          _title: obj.title,
          id: obj.id,
          latitude: obj.location.lat,
          longitude: obj.location.lng,
          iconPath: iconPath, //图标路径
          width: 38,
          height: 40,
          address: obj.address
        })
      }
      if (res.status == "0") {
        if (callback && typeof callback === 'function') {
          callback(mks);
        }
      }
    },
    fail: function (res) {
      console.log(res);
    }
  });
}

module.exports = {
  calculateDistan: calculateDistance,
  getDetailedAdrress: detailedAdrress,
  routePlanning: routePlanning,
  keywordSearch: keywordSearch,
  direction,
  getCoord
}