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 get all the distances from point 1 to point 2 to the destination #646

Closed elangabdulazis closed 2 years ago

elangabdulazis commented 2 years ago

hi I want to ask if there is a distance calculation using a leaflet? because I need distance data from latitude 1,2,3 to finish

curtisy1 commented 2 years ago

The plugin itself does not perform any distance calculation. The distance between the waypoints is returned by the respective routing api (i.e. OSRM).

If you just need the distance between two LatLngs, you could use Leaflet's builtin distanceTo function

elangabdulazis commented 2 years ago

can i apply a* algorithm to route finding in leaflet?

curtisy1 commented 2 years ago

Not sure I get the question. Is it that you want to use your own routing provider? The plugin itself only uses different backend services and formats the returned waypoints. So if you want different results, you would either have to use a different routing provider or build your own (see osrmv1 for reference)

It would also help if you could share some code or runnable example, so I can understand what exactly is going on

elangabdulazis commented 2 years ago

I also want to ask how in the leaflet to get distance data?

elangabdulazis commented 2 years ago

I also want to ask how in the leaflet to get distance data?

curtisy1 commented 2 years ago

See my first reply on how to use distanceTo for getting the distance between two LatLngs.

All this plugin does is request the distance data from an API of your choice and format it accordingly. It does not do any distance calculation itself.

If that is not your issue, please provide a code snippet or codesandbox/etc. that shows how your question is related to leaflet-routing-machine. Otherwise I can't accurately help you.

tmbp95 commented 2 years ago

Hello all, I'm not sure if this is the right place to question this, but I believe it kinda relates so, here it is: Is there a method to access the private object _selectedRoute? I'm trying to get the summary from it because I will need to get the distance and duration of the selected route.

Thank you, Best regards

curtisy1 commented 2 years ago

Let's say you have something like the following

const routingControl = L.Routing.control(...);

then you could do something along the lines of

const selectedRoute = routingControl._selectedRoute;

alternatively, you can provide your own _routeSelected implementation

const RoutingControl = L.Routing.Control.extend({
    _routeSelected: function() {
        // trigger some custom logic here to work with the selected route, then call the base implementation
    }
});

This is supposing you use plain JavaScript though. If you're using Typescript, those types won't be available, unless you convert it to any or use a custom type

curtisy1 commented 2 years ago

@elangabdulazis @tmbp95 Can I go ahead and mark this question as resolved?

tmbp95 commented 2 years ago

Yes, thank you!