0

我有一个 Mapbox GL 地图,其中有一个图层和该图层上的多个标记,我正在尝试使用 Directions GL 插件在我的应用程序中绘制路线 + 显示路线信息(从起点到目的地的距离/时间/路线)。不幸的是,除了设置起点/目的地(如下所示)以在我的地图上显示路线+路线数据之外,我找不到任何信息。我能找到的唯一可用信息是MapBox GL 行车路线示例中提到的信息,但这不是我真正想要的,因为我不想将起点/目的地显示为 A 和 B 点,也不想显示 A/B 点搜索框,如上面的 mapbox.com 示例。

有人可以告诉我我在这里缺少什么以及如何在起点/目的地之间绘制路线,使用 Mapbox GL 插件显示路线信息来帮助我吗?谢谢

  var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v8', 
    center: [userCoordinates.coords.longitude, userCoordinates.coords.latitude],
    zoom: 15
  });


  var directions = new mapboxgl.Directions({
    unit: 'metric',
    profile: 'driving'        
  });



  directions.setOrigin([userCoordinates.coords.longitude, userCoordinates.coords.latitude]);


  map.on('click', function(e) {

    var features = map.queryRenderedFeatures(e.point, { layers: ['gsLayer'] });
    if (!features.length) {
      return;
    }
    var feature = features[0];

    directions.setDestination([feature.geometry.coordinates[0], feature.geometry.coordinates[1]]);

  });
4

1 回答 1

2

听起来您根本不想使用插件,而是直接向Directions API发出请求。

我建议看一下mapbox-sdk-js - 一个用于发出客户端请求的有用 js 库。可以在此处找到路线的 API 文档。

于 2016-04-07T03:09:43.707 回答