I currently have a function drawing a map line when clicking on an option, however I only want one to be displayed at a time, so that when the user clicks on another option, it erases the previously drawn route, then draws a new one. I've tried to research how to do this but have come up with nothing.
function addRouteShapeToMap(route) {
route.sections.forEach((section) => {
let linestring = H.geo.LineString.fromFlexiblePolyline(section.polyline);
let polyline = new H.map.Polyline(linestring, {
style: {
lineWidth: 8,
strokeColor: "rgba(207, 0, 15, 1)",
},
});
map.addObject(polyline);
});
}
You should be able to remove the polyline, or generally any added map object, with the method removeObject. This assumes you keep a reference to the polyline outside the function which adds it to the map, or else you could iterate over map objects retrieved with map.getObjects to find it.
map.removeObject(polyline);
See the API Reference