javascript - Mapbox-gl-directions 事件不会触发

标签 javascript routes mapbox mapbox-gl-js

我正在尝试计算 mapbox-gl-directions 中两个标记之间的简单路线。我想使用.on('route')事件,但它永远不会触发。我尝试重建 docs 中所示的示例,但没有成功。我做了以下事情:

var map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/mapbox/streets-v8'
});


var directions = new mapboxgl.Directions({
  unit: 'metric', // Use the metric system to display distances.
  profile: 'driving', // Set the initial profile to walking.
  container: 'directions' // Specify an element thats not the map container.
});


map.on('load', function() {
  directions.setOrigin([77.5045504332, 13.0386169339]); // On load, set the origin to "Toronto, Ontario".
  directions.setDestination([77.5075504332, 13.0386169339]); // On load, set the destination to "Montreal, Quebec".
});


directions.on('route', function(e) {
  alert("FIRE!")
});

为什么这个事件没有被触发? 这是JS FIDDLE也。

最佳答案

通过更新外部资源链接以使用最新版本的 Mapbox GL 和 Mapbox GL Directions 库,我能够让您的代码在您提供的 JS Fiddle 中运行。这意味着 Mapbox GL JS 的 v0.12.0 更改为 v0.26.0,Directions 插件的 v1.0.0 更改为 v2 .2.0.

<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.css' rel='stylesheet' />

<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v2.2.0/mapbox-gl-directions.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v2.2.0/mapbox-gl-directions.css' type='text/css' />

它看起来像您在 docs 中找到的示例自2015年12月发布以来一直没有更新; here's a link到一个更新的方向示例,并使用两个库的正确版本。最后,我可能会删除 Mapbox.js 的外部链接,因为使用 Mapbox GL JS 时不需要它们。祝你好运!

关于javascript - Mapbox-gl-directions 事件不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40197437/

相关文章:

javascript - 选择选项时禁用选择下拉列表组中的选择选项

mapbox - 如何使用 loadImage 在 mapbox 中添加 svg 作为标记?

reactjs - 将组件作为 prop 传递给 Route 与在渲染函数中包装组件之间的区别

ruby-on-rails - 没有路由与嵌套资源匹配

javascript - Backbone 路由器

java - Mapbox map 在 Android 上不显示

java - 如何在我的 Mapbox 图层的左上角添加导航菜单和抽屉

javascript - 拆分路径名获取路由参数

javascript - 依赖注入(inject) Angular 2

javascript - JS 如何从 javascript 更改 css 类颜色?