perliedman / leaflet-routing-machine

Control for routing in Leaflet
https://www.liedman.net/leaflet-routing-machine/
Other
1.06k stars 347 forks source link

How to simply draw a route without controls and etc #651

Open ntaraujo opened 2 years ago

ntaraujo commented 2 years ago

Hi, I can't find on docs the way to do so, it's simple but I can't figure it out. Can someone give me an example of L.Routing.Line?

curtisy1 commented 2 years ago

Sorry for the late reply @ntaraujo!

If you just want to draw a polyline, you can do so using the official Leaflet docs. That's basically all L.Routing.Line does as well

If you want to use L.Routing.Line instead, you should be able to do something like this:

const line = new L.Routing.Line({coordinates: [L.LatLng, ...]});
map.addLayer(line);
betaWeb commented 1 year ago

@curtisy1 hey, your solution does not work ! I've an error Uncaught TypeError: Cannot read properties of undefined (reading 'length') on method _findWaypointIndices :

for (i = 0; i < wps.length; i++) {
  indices.push(this._findClosestRoutePoint(wps[i].latLng));
}

wps var is undefined, so it doesn't work :/

curtisy1 commented 1 year ago

Yes and no. For a simple route that should only be displayed on a map, it does work. If you want interactivity, like touching the line, you are correct that this isn't covered by the example I provided.

However, you can simply pass an inputWaypoints object into the initialization and it should work.

If the issue persists, please open another issue and I will have a look at it.