Raruto / leaflet-elevation

Leaflet plugin that allows to add elevation profiles using d3js
GNU General Public License v3.0
255 stars 85 forks source link

_clearPath() leaves line on the map #31

Closed Beamer92 closed 4 years ago

Beamer92 commented 4 years ago

When I call .clear() on my terrain profiler, the graph clears out but the line get's left on the map. I can see where _clearPath() goes and removes class names from the <path> element, but this doesn't appear to affect the line on the map at all. So I'm unsure what the objective is.

I would expect the polyline that's drawn when I load in data to disappear (if not remove the g->svg->path elements altogether).

If this is expected behavior, I'd like to remove the line when I clear the profile data in preparation to display a new profile and line. I found that I can remove the graph from the map entirely with .remove(), but the line persists.

Raruto commented 4 years ago

Hi Brendan, have you already tried to do something like this? https://github.com/Raruto/leaflet-elevation/issues/13#issuecomment-507677950

Beamer92 commented 4 years ago

I'm sure I looked through old issues before asking... Yes that worked for me! I danced around it for so long yesterday.

The solution for me is to use .remove() in two places:

terrainProfile.addTo(theMap);
terrainProfile.load(geoJsonData);

...stuff happens....

terrainProfile.layer.remove() //removes line from map
terrainProfile.remove(); //removes graph 

Thanks!

Raruto commented 4 years ago

Ok, good to know, this is necessary because the load / loadGeoJSON functions can be actually considered a shorthand for the following:

var chart = L.control.elevation();
var layer = L.geoJson( jsonData, {
                onEachFeature: chart.addData.bind( chart ) // populate graph with elevation data
            });

...

chart.addTo(map); // add graph to map
layer.addTo(map); // add line to map

...

layer.remove() // remove line from map
chart.remove(); // remove graph from map

Source code: https://github.com/Raruto/leaflet-elevation/blob/447201b70a50bacf432da2e21ab74c9083d34bbb/leaflet-elevation.js#L297

Have a nice day, Raruto