我不是很擅长编码,目前使用mapbox API来创建一个带点的地图。但是我在使用"geolocate“的用户位置和我地图上的点之间的基本路由选项上找不到任何东西。在API中有什么方法可以做到这一点吗?我想为用户创建一个选项,让他们找到我目前在地图上的位置以及这些位置之间的路径。您的帮助我们将不胜感激。
问候
阿尔
发布于 2014-03-20 08:33:02
Mapbox api中没有可用路由,该api已准备好投入生产。他们正在做一个预览请看这里:https://www.mapbox.com/developers/api/directions/
mapbox的一位团队成员确实提出了一个替代方案。查看此处:https://stackoverflow.com/a/16305757/475882
发布于 2015-10-07 05:28:27
我使用Mapbox.directions插件来做这件事。我使用map contextmenu事件来获取某个位置的右键单击事件(我的标记具有clickable: false,因此地图将获得鼠标单击)。我使用MouseEvent数据来获取延迟,并将其设置为路由的起点或目的地。我允许插件查询路线,并使用控件在地图上显示路线、说明和突出显示的路径。以下是一些代码片段:
$('#lblStatus').html("Calculating route....");
// **** units is not working yet in the current Mapbox release
moDirections = L.mapbox.directions({
profile: 'mapbox.driving',
units: 'metric'
});
moDirections.on('load', function (directions) {
// Loop through all route coordinates and determine bounds for route.
var minLat = 90, minLng = 180, maxLat = -90, maxLng = -180;
var lat, lng;
directions.routes[0].geometry.coordinates.forEach(function (laCoordinate, index) {
lat = laCoordinate[1];
lng = laCoordinate[0];
if (lat < minLat) {
minLat = lat;
}
if (lng < minLng) {
minLng = lng;
}
if (lat > maxLat) {
maxLat = lat;
}
if (lng > maxLng) {
maxLng = lng;
}
});
var loBounds = L.latLngBounds(L.latLng(minLat, minLng), L.latLng(maxLat, maxLng));
moMap.fitBounds(loBounds);
$('#lblStatus').html("");
});
moDirections.setOrigin(loStart);
moDirections.setDestination(loEnd);
moDirections.query();
moDirectionsLayer = L.mapbox.directions.layer(moDirections).addTo(moMap);
moDirectionsErrorsControl = L.mapbox.directions.errorsControl('pnlRouteErrors', moDirections);
moDirectionsRoutesControl = L.mapbox.directions.routesControl('pnlAlternateRoutes', moDirections);
moDirectionsInstructionsControl = L.mapbox.directions.instructionsControl('pnlRouteInstructions', moDirections);
上面的pnl*元素是注入控件的div。
目前,基本上还没有directions插件的文档。您可以在此处获得开放源代码:https://github.com/mapbox/mapbox-directions.js
唯一的例子是:https://www.mapbox.com/mapbox.js/example/v1.0.0/mapbox-directions/,但是我发现输入控件不能很好地工作,不适合我的设计。
https://stackoverflow.com/questions/22364507
复制相似问题