I need to remove the Direction paths drawn on Google map? I know how to remove markers, but when you remove markers path is not getting cleared. Is there a way to remove the direction path than creating map from the beginning again?
A:
Yes, it's the same way as removing the markers (Google is using the setMap as a standard now). A good practice is always define a global array variables for your markers, polylines and directions..etc:
var markers = [];
var drArr = [];
var pArr = [];
...
// whenever you set a marker, add it to the markers array
markers.push(marker);
...
// whenever you create a DirectionsRenderer instance, add it to the DirectionsRenderer array
drArr.push(directionsRenderer);
...
// whenever you create a Polyline instance, add it to the Polyline array
pArr.push(polypath);
// This is used to reset the map
function resetMap() {
// remove Markers
for(i=0; i < markers.length; i++)
markers[i].setMap(null);
// remove DirectionsRenderers
for(i=0; i < drArr.length; i++)
drArr[i].setMap(null);
// remove Polylines
for(i=0; i < pArr.length; i++)
pArr[i].setMap(null);
}
as you can see, you can seperate the resetMap function to functions (like resetMarkers..etc).
Hope this help.
ifaour
2010-10-22 14:24:28