3

I am using nominatim for leaflet routing. The routing works perfectly as i want-a user can input from and destination location in search box and the map shows the route between the two points as in the picture below. enter image description here

But I want to get the coordinates of the destination location. Is there any way i can do this ? Below is code sample how i have added map to my page.

var map = L.map('map').setView([60.4500, 22.2667], 8);


    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map)


    L.Routing.control({
        waypoints: [
            //L.latLng(60.323935,22.344035)

        ],

        geocoder: L.Control.Geocoder.nominatim()


    }).addTo(map);
Stranded Kid
  • 1,347
  • 3
  • 15
  • 23
user3303274
  • 609
  • 2
  • 6
  • 20

2 Answers2

2

Look at the RoutingResultEvent. It will be called each time a route has been successfully calculated. The event the handler will receive contains the waypoints used for the route.

So basically

var x = L.Routing.control({
    // YOUR STUFF
    geocoder: L.Control.Geocoder.nominatim()
}).addTo(map);
x.on("routesfound", function(e) {
    var waypoints = e.waypoints || [];
    var destination = waypoints[waypoints.length - 1]; // there you have the destination point between your hands

});
Stranded Kid
  • 1,347
  • 3
  • 15
  • 23
  • I tried above method. It results in an array - [object,object], but not the actual co-ordinates. The waypoints also results in [object,object],[object,object]. How can i get the actual coordinates out of it ? – user3303274 Dec 08 '15 at 08:00
  • You mean, e.waypoints returns [object,object]? Then waypoints[waypoints.length - 1] will return you an object, and this object will contain what you want I guess. http://www.liedman.net/leaflet-routing-machine/api/#l-routing-waypoint. In my example, destination.latLng will returns you the lat/lng – Stranded Kid Dec 09 '15 at 09:08
0

you can use:

routeControl.on("routesfound", function(e) {
    coordinates=e.routes[0].coordinates;
    destination=coordinates[coordinates.length-1];
});

there you have the coordinates, the waypoints are not the same as the coordinates, what you are looking for is for the coordinates of the route found, and not for the waypoints that you have asked for, then you can take the coordinates.lenght-1 and there you will have what you wanted

vmrvictor
  • 607
  • 6
  • 24