How do should I hide the Leaflet Routing Machine route and markers on mouseout event?
Currently, I have regular Leaflet marker, that has mouseover/mouseout events. When mouseover event is fired and route belonging to that marker is created using L.Routing.control
route = L.Routing.control({
show: false,
addWaypoints: false,
draggableWaypoints: false,
fitSelectedRoutes: false,
waypoints: [
L.latLng(locations.aLat, locations.aLng), // Hardcoded coordinates
L.latLng(locations.bLat, locations.bLng), // Hardcoded coordinates
],
});
route.addTo(map);
When mouseout event is triggered I call route.remove()
which deletes the route completely.
The problem with this approach:
- It takes a little bit less than half a second to retrieve route from OSRM server
- Every single mouseover event sends a request to an OSRM server
I have tried to set route opacity to 0 and marker icon size to 0 on mouseout event, however, you cannot dynamically change styles in Leaflet Routing Machine.
Another approach I took was to give css class names to routes and try to set display: none
but assigning classes to many different routes is a really messy way to solve this.
The demo is quite neat if you want to play around.(Updated works as supposed to) Grab it здесь