前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mapboxGL和高德API结合实现路径规划

mapboxGL和高德API结合实现路径规划

作者头像
lzugis
发布2020-04-08 15:55:01
1.8K0
发布2020-04-08 15:55:01
举报

概述

本文将mapboxGL和高德地图API结合起来,实现路径规划。

效果

效果
效果

实现

API
API

高德地图路径规划API说明如上图,有行走、公交、驾车等多种路径,本文以行走为例来说明。

实现逻辑
实现逻辑
  1. 添加点、线图层
map.on('load', function() {
  var geojson = {
    'type': 'FeatureCollection',
    'features': []
  };
  map.addSource('path', {
    type: 'geojson',
    data: geojson
  });
  map.addSource('points', {
    type: 'geojson',
    data: geojson
  });
  map.addLayer({
    id: 'path',
    type: 'line',
    source: 'path',
    'paint': {
      'line-color': '#4ddc26',
      'line-width': 5
    }
  });
  map.addLayer({
    id: 'points',
    type: 'circle',
    source: 'points',
    'paint': {
      'circle-color': [
        'match',
        ['get', 'type'],
        '起', '#62b500',
        '#f54336' // 无匹配值
      ],
      'circle-radius': 13
    }
  });
  map.addLayer({
    'id': 'label',
    'type': 'symbol',
    'source': 'points',
    'layout': {
      'text-field': ['get', 'type'],
      "text-size": 12
    },
    paint: {
      'text-color': '#ffffff'
    }
  })
})
  1. 开始路径查询
startDraw() {
  that.isDraw = true;
  that.points = [];
  map.getCanvas().style.cursor = 'crosshair';
  var geojson = {
    'type': 'FeatureCollection',
    'features': []
  };
  map.getSource('path').setData(geojson);
  map.getSource('points').setData(geojson);
}
  1. 注册点击事件
map.on('click', e => {
  var lngLat = e.lngLat;
  if(that.isDraw) {
    that.points.push([lngLat.lng, lngLat.lat]);
    that.drawPoints();
    if(that.points.length === 2) {
      that.getRoute();
    }
  }
});
  1. 路径查询与渲染
getRoute() {
  that.isDraw = false;
  map.getCanvas().style.cursor = '';
  const url = 'https://restapi.amap.com/v3/direction/walking';
  var start = that.points[0].map(res => {
    return res.toFixed(5);
  });
  var end = that.points[1].map(res => {
    return res.toFixed(5);
  });
  var params = {
    key: that.key,
    origin: start.join(','),
    destination: end.join(',')
  };
  $.get(url, params, res => {
    that.paths = res.route.paths;
    var geojson = {
      'type': 'FeatureCollection',
      'features': []
    };
    for(var i = 0;i<that.paths.length;i++) {
      var steps = that.paths[i].steps;
      for(var j = 0;j<steps.length;j++) {
        var step = steps[j];
        var polyline = step.polyline;
        polyline = polyline.split(';');
        polyline = polyline.map(p => {
          return p.split(',').map(Number);
        });
        var feat = {
          type: 'Feature',
          properties: {},
          geometry: {
            type: 'LineString',
            coordinates: polyline
          }
        };
        geojson.features.push(feat);
      }
    }
    map.getSource('path').setData(geojson);
  })
},
drawPoints() {
  var geojson = {
    'type': 'FeatureCollection',
    'features': []
  }
  for(var i = 0;i<that.points.length;i++) {
    var type = i=== 0? '起' : '终';
    var p = that.points[i];
    geojson.features.push({
      type: 'Feature',
      geometry: {
        type: 'Point',
        coordinates: p
      },
      properties: {
        'type': type
      }
    })
  }
  map.getSource('points').setData(geojson);
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 效果
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档