简体   繁体   English

Mapbox通过两个标记绘制方向

[英]Mapbox draw direction via two markers

How can i draw route direction from start to finish if I have coordinates of this two points? 如果我有这两点的坐标,我如何从开始到结束绘制路线方向? Now my code looks like this and it just gave me 2 static markers on map 现在我的代码看起来像这样,它只是在地图上给了我2个静态标记

var map = L.mapbox.map('map', 'mapbox.streets', {
    zoomControl: false
}).setView([start.lat, start.lng], 12);
map.attributionControl.setPosition('bottomleft');
var directions = L.mapbox.directions({
    profile: 'mapbox.walking'
});
var directionsLayer = L.mapbox.directions.layer(directions).addTo(map);
L.marker([start.lat, start.lng], {}).addTo(map);
L.marker([finish.lat, finish.lng], {}).addTo(map);

If I understand correctly, you wish to use Mapbox's direction and routing layer to get the walking route between the two points and display it. 如果我理解正确,您希望使用Mapbox的方向和路由图层来获取两点之间的步行路径并显示它。 To do so you need to set the origin and destination points and call the query() function of direction . 为此,您需要设置原点和目标点并调用directionquery()函数。 You also need to add a routes control to the map. 您还需要向地图添加路径控件。 The revised code is as follows. 修订后的代码如下。

// example origin and destination
var start = {lat: 22.3077423, lng: 114.2287582}; 
var finish = {lat: 22.3131334, lng: 114.2205973};

var map = L.mapbox.map('map', 'mapbox.streets', {
    zoomControl: false }).setView([start.lat, start.lng], 14); 

map.attributionControl.setPosition('bottomleft'); 
var directions = L.mapbox.directions({
    profile: 'mapbox.walking' 
});

// Set the origin and destination for the direction and call the routing service
directions.setOrigin(L.latLng(start.lat, start.lng)); 
directions.setDestination(L.latLng(finish.lat, finish.lng));   
directions.query(); 

var directionsLayer = L.mapbox.directions.layer(directions).addTo(map); 
var directionsRoutesControl = L.mapbox.directions.routesControl('routes', directions)
    .addTo(map);

You may not need to add the origin / destination markers yourself, as origin / destination markers would be displayed as part of the directions control. 您可能不需要自己添加原点/目标标记,因为原点/目标标记将显示为路线控制的一部分。

You'll need a polyline for that. 你需要一条折线。 Leaflet's (Mapbox is an extended version of Leaflet) class L.Polyline is what you need: Leaflet(Mapbox是Leaflet的扩展版本)类L.Polyline是您所需要的:

Reference: http://leafletjs.com/reference.html#polyline 参考: http//leafletjs.com/reference.html#polyline

Code: 码:

var polyline = new L.Polyline([
    [start.lat, start.lng],
    [finish.lat, finish.lng]
]).addTo(map);

Example on Plunker: http://plnkr.co/edit/2XSeS1?p=preview 关于Plunker的例子: http ://plnkr.co/edit/2XSeS1?p = preview

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

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