Skip to content
Advertisement

React-Leaflet/React-Routing-Machine: Remove route and waypoints

I am using the following packages for my map:

JavaScript

Essentially I have a Routing machine component which I’ve integrated with my Map and Markers i.e. (upon clicking two points on the map you get the route) you can drag each and the route updates!

However at this point I have a button which resets everything, clears the markers, the associated LAT & LONG. But I just want the route to reset as well.

enter image description here

You can see those previous routes (in beautiful “chartreuse”) stay on the map.

Right now I have a function which is supposed to control when the component is visible:

JavaScript

This is my Routing Machine:

JavaScript

Actually I logged something in the updateLeafletElement function and zzilch.

JavaScript

I got rid of the the code that’s not important to the problem at hand!

Thanks in advance!

Advertisement

Answer

I actually wound up solving it myself!

Think the head scratcher was while react-leaflet has its lifeCycle methods, i.e. createLeaflet, updateLeafletElement you should not forget the regular React life methods!

Not sure if they overlap, but I found adding componentDidMount:

JavaScript

with updateLeafletElement (I’m using the API for the function correctly now) it accepts a fromProps and toProps to just check the value of the prop I pass to the Routing Machine…

JavaScript

Finally on unmount, use the removeControl method on the map you pass into the Routing Machine to remove the Router-Machine!

JavaScript

Hope this helps! FYI: this is the app I’m using the Routing Machine in case you’d like to browse the other integrations…

Advertisement