高德地图api——通过经纬度计算路线

最近在使用高德地图绘制高速路线,将通过经纬度和高速路线桩号计算路线数据后,将数据绘制在地图上进行展示,在此将封装的方法做下记录,将js引入后直接调用方法即可使用。

//构造工具函数
export class Converter {
  constructor() {
    this.queryKmPile = function (e, r) {
      if (!e[0].path)
        return false;
      (r = r.toLowerCase()), (this.data = e);
      var t = this._kmPile2m(r), i = e.filter(function (e) {
        var r = e.path[0], i = e.path[e.path.length - 1];
        return r[2] <= t && i[2] >= t;
      });
      if (!i[0]) return
      var n = i[0], o = t / 1e3;
      return t % 1e3 == 0 && n[o] ? n[o] : this._computeDistanceOfPoint(i[0].path, t);
    };
    this._kmPile2m = function (e) {
      if (-1 == e.indexOf('+') && -1 == e.indexOf('k')) return
      var r = e.split('+'), t = r[0].split('k')[1];
      return 1e3 * Number(t) + Number(r[1]);
    };
    this._computeDistanceOfPoint = function (e, r) {
      var t = e.filter(function (e) {
        return e[2] <= r;
      }), i = e.filter(function (e) {
        return e[2] >= r;
      }), n = t[t.length - 1], o = [n, i[0]], a = r - n[2];
      return this._distanceOfPoint(o, a);
    };
    this._distanceOfPoint = function (e, r) {
      var t = e[0], i = e[1], n = r / (i[2] - t[2]), o = i[0] - t[0], a = i[1] - t[1], u = o * n + t[0], l = a * n + t[1];
      return [Number(u.toFixed(6)), Number(l.toFixed(6))];
    };
  }
}

/**
 * @param {any}
 * @returns {Array}
 * editor fuhy
 */
//通过公里桩号和起止经纬度计算路线数据,sl,sp起始经纬度数组,起始桩号,el,ep结束经纬度数组,结束桩号,AMap地图对象
export async function getLineData(sl, el, sp, ep, AMap) {
  const data = await wayData(sl, el, AMap);
  let path = data.routes[0].steps.map((p) => {
    return p.path.map((lngLat) => {
      return [lngLat.lng, lngLat.lat];
    });
  });
  let pathArr = [];
  path.forEach((p, i) => {
    if (!i) {
      p.shift();
    }
    pathArr = pathArr.concat(p);
  });
  pathArr[0] = sl;
  pathArr[pathArr.length - 1] = el;
  if (!sp.includes('+') && !sp.includes('k')) return
  const sm =
    Number(sp.split('+')[0].split('k')[1]) * 1000 + Number(sp.split('+')[1]);
  const pathData = pathArr.map((p) => {
    return [p[0], p[1]];
  });
  if (!ep.includes('+') && !ep.includes('k')) return
  const endm =
    Number(ep.split('+')[0].split('k')[1]) * 1000 + Number(ep.split('+')[1]);
  const j = pathData[pathData.length - 1];
  if (endm > j[2]) {
    const dm = endm - j[2];
    const addm = dm / pathData.length;
    pathData.forEach((e, i) => {
      e[2] = e[2] + i * addm;
    });
  }
  return pathData;
}
//通过经纬度计算路线数据,s起始经纬度数组,e结束经纬度数组,AMap地图对象
function wayData(s, e, AMap) {
  return new Promise((resolve, reject) => {
    AMap.plugin('AMap.Driving', function () {
      const d = new AMap.Driving({
        // DrivingPolicy.LEAST_TIME最快捷模式
        policy: AMap.DrivingPolicy.LEAST_TIME,
      });
      d.search(s, e, function (status, result) {
        resolve(result);
      });
    });
  });
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用以下代码进行高德经纬度距离计算和驾驶距离计算: ```php //高德地图api获取两地经纬度 $origin = "北京市朝阳区阜通东大街6号"; $destination = "北京市海淀区中关村南大街甲2号"; $origin_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($origin); $destination_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($destination); $origin_json = file_get_contents($origin_url); $origin_data = json_decode($origin_json,true); $origin_location = explode(",",$origin_data['geocodes'][0]['location']); $destination_json = file_get_contents($destination_url); $destination_data = json_decode($destination_json,true); $destination_location = explode(",",$destination_data['geocodes'][0]['location']); $origin_lng = $origin_location[0]; $origin_lat = $origin_location[1]; $destination_lng = $destination_location[0]; $destination_lat = $destination_location[1]; //高德经纬度距离计算 $distance_url = "https://restapi.amap.com/v3/distance?key=YOUR_KEY&origins={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}&type=1"; $distance_json = file_get_contents($distance_url); $distance_data = json_decode($distance_json,true); $distance = $distance_data['results'][0]['distance']; //高德地图api计算驾驶距离 $driving_url = "https://restapi.amap.com/v3/direction/driving?key=YOUR_KEY&origin={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}"; $driving_json = file_get_contents($driving_url); $driving_data = json_decode($driving_json,true); $duration = $driving_data['route']['paths'][0]['duration']; $distance_driving = $driving_data['route']['paths'][0]['distance']; ``` 请将上述代码中的 `YOUR_KEY` 替换为你的高德地图 API 密钥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大魔王逍遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值