如果您查看此问题,您会发现您对不同标记图标的问题已经得到解答。
的createMarker
选项功能L.Routing.control
可以像这样使用:
// source: https://github.com/pointhi/leaflet-color-markers
var greenIcon = new L.Icon({
iconUrl: 'https://cdn.rawgit.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png',
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.4/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
L.Routing.control({
waypoints: [
L.latLng(57.74, 11.94), // startmarker
L.latLng(57.6792, 11.949) // endmarker
],
createMarker: function(i, wp, nWps) {
if (i === 0 || i === nWps - 1) {
// here change the starting and ending icons
return L.marker(wp.latLng, {
icon: greenIcon // here pass the custom marker icon instance
});
} else {
// here change all the others
return L.marker(wp.latLng, {
icon: yourOtherCustomIconInstance
});
}
}
}).addTo(map);
演示- 在隐身窗口中打开它,因为 API 存在请求限制。
您应该看到如下内容:
更新:要动态更改路线,您必须这样做:
将您的路由控制实例存储到一个变量中:var control = L.Routing.control({...})
然后像这样更改标记位置:
// this is the starting marker latitude
control.getRouter().options.waypoints[0].lat = L.latLng(59.74, 11.94);
// similarly for longitude and for ending marker to change the position dynamically
然后刷新路由图:
control.route();