简体   繁体   中英

how to access _selectedRoute leaflet?

how can I access this _selectedRoute in my code? i need it to save instructions and summary

let routing = L.Routing.control({
    waypoints: [
        L.latLng(20.97912897266421, 105.78617941902107),
        L.latLng(21.0033696728394, 105.82059752419705)
    ],
    routeWhileDragging: true,
    geocoder: L.Control.Geocoder.nominatim()
}).addTo(mymap);

console.log(routing)
console.log(routing._selectedRoute)

it said undefined when I console.log it, thank you

reference: browser logs image

Finding the route is an asynchronous operation, it will start when you create the routes control (or when waypoints are changed), but it will complete at some later point. This means that the route will not be available immediately after the control is created.

This is common in JavaScript, a lot of operations are asynchronous and you normally deal with it either by callbacks or event handlers, you should look into familiarizing yourself with this concept.

In this particular case, you should not use _selectedRoute , the underscore indicates it is an internal variable not intended for public use. Instead, you should use the routeselected event handler to perform whatever action you need when a route is selected:

routing.on('routeselected', function(e) {
   var route = e.route
   // Your action goes here
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM