I have a sample google map here where I can draw a polyline and drag the marker to redraw the polyline. When I'm dragging the marker, I'm usingPath.setMap(null)
to redraw the Polyline as,
google.maps.event.addListener(marker, 'dragend', function(event) {
var newLatLng = event.latLng;
var index = Latlngs.map(function(element) {
return element[0];
}).indexOf(marker.id);
if (index !== -1) {
Latlngs[index] = [marker.id, newLatLng];
}
console.log(Latlngs);
var changedLine=[];
for (var i = 0; i < Latlngs.length; i++) {
changedLine.push(Latlngs[i][1]);
}
console.log(changedLine);
Path.setMap(null);
draw(changedLine, map);
});
But the polyline is not clearing as required. How can I clear old path and redraw the same?
The issue is the creation of the markers. When you create a new Marker you call
draw
without removing the existingPolyline
(Path
)This would fix it:
But there is no need to draw a new Polyline, polylines-pathes may be modified directly: