简体   繁体   English

如何使用 Leaflet Routing Machine 和 Mapzen 获得行人路线?

[英]How to get a pedestrian route using Leaflet Routing Machine and Mapzen?

I know Mapzen can provide a pedestrian route for the itinerary I am trying to do because I can get it on openstreetmaps.org.我知道 Mapzen 可以为我正在尝试的行程提供步行路线,因为我可以在 openstreetmaps.org 上找到它。

But I can't get it to work on my embedded map, and I absolutely don't know the reason.但是我不能让它在我的嵌入式地图上工作,我绝对不知道原因。

My code is the following:我的代码如下:

L.Routing.control({
waypoints: [
    L.latLng(-44.004358, 170.476709),
    L.latLng(-43.985844, 170.464058)
],
router: L.Routing.Mapzen('valhalla-apikey', 'pedestrian')
// formatter: new L.Routing.Mapzen.Formatter()
}).addTo(cafeMap);

(Replacing apikey with my apikey) (用我的 apikey 替换 apikey)

But all I get is the normal driving itinerary.但我得到的只是正常的驾驶行程。 I tried replacing 'pedestrian' by all the other options available, but can't get it to work.我尝试用所有其他可用选项替换“行人”,但无法使其正常工作。

Anyone sees a glitch in my code?有人发现我的代码有问题吗?

Thanks谢谢

You are initializing the router incorrectly.您正在错误地初始化路由器。 The plugin follows the convention described in the Leaflet docs for class factories .该插件遵循类工厂的 Leaflet 文档中描述的约定。 So you can either use the new operator to create a new instance of the router class:因此,您可以使用new运算符创建路由器类的新实例:

router: new L.Routing.Mapzen('valhalla-apikey', 'pedestrian')

or the lowercase factory method, which does the same thing:或小写工厂方法,它做同样的事情:

router: L.Routing.mapzen('valhalla-apikey', 'pedestrian')

You must also specify a formatter so that routing machine can parse the directions returned from mapzen.您还必须指定一个格式化程序,以便路由机器可以解析从 mapzen 返回的方向。 The full code for the routing control would thus be:因此,路由控制的完整代码将是:

var control = L.Routing.control({
  waypoints: [
    L.latLng(-44.004358, 170.476709),
    L.latLng(-43.985844, 170.464058)
],
  waypointMode: 'snap',
  router: new L.Routing.Mapzen('valhalla-apikey', 'pedestrian'),
  formatter: new L.Routing.Mapzen.Formatter()
}).addTo(map);

The way you are doing it (without the new operator) is passing an undefined value to the routing control, and the results you are getting are actually from the OSRM router.你这样做的方式(没有 new 运算符)是将一个未定义的值传递给路由控制,你得到的结果实际上来自 OSRM 路由器。 So it looks like Leaflet Routing Machine defaults to OSRM if given an undefined value for the router.因此,如果为路由器提供未定义的值,则看起来 Leaflet Routing Machine 默认为 OSRM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM