0

I have the next code which show a path using a polyline. How can I remove it?

downloadUrl("myfile.asp", function(data) {
var xml = xmlParse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
var path = [];
for (var i = 0; i < markers.length; i++) {
  var lat = parseFloat(markers[i].getAttribute("lat"));
  var lng = parseFloat(markers[i].getAttribute("lng"));
  var point = new google.maps.LatLng(lat,lng);
  path.push(point);
}//finish loop

var polyline = new google.maps.Polyline({
  path: path,
  strokeColor: "#FF0000",
  strokeOpacity: 1.0,
  strokeWeight: 2
});
polyline.setMap(map);

}); //end download url

I have tried it using the next function but I m not able to make it work.

 function removePath() {
 polyline.setMap(null)

}

Fran Rod
  • 586
  • 4
  • 14
  • 26

1 Answers1

2

I think the problem is position of the variable "polyline".

var polyline = null;
downloadUrl("myfile.asp", function(data) {
   ...

   polyline = new google.maps.Polyline({
      path: path,
      strokeColor: "#FF0000",
      strokeOpacity: 1.0,
      strokeWeight: 2
   });
   polyline.setMap(map);

}); //end download url

function removePath() {
  polyline.setMap(null)
}
wf9a5m75
  • 6,100
  • 3
  • 25
  • 59
  • You know, there two scopes of programming; local variable and global variable. Javascript scope works based on each function. So if you want to use the variable "polyline" in another function except downloadUrl callback, you need to define outside of function as global variable. – wf9a5m75 Nov 21 '12 at 17:13
  • Working! I'm very gratefully ! – Fran Rod Nov 21 '12 at 22:46
  • Hello again. When I try to retrieve my asp page with parameters to get another markers once a time I have already remove the path, it comes back to load the old path with the new one. How can I remove completely the old path to load the new one? – Fran Rod Nov 22 '12 at 18:07
  • polyline.setMap(null); then polyline = null; – wf9a5m75 Nov 22 '12 at 18:12
  • I have added that line below polyline.setMap(null); but it still load the old path – Fran Rod Nov 22 '12 at 18:47
  • 1
    It is working now, I commented the path var and I uncommented it and added the polyline = null; and works – Fran Rod Nov 25 '12 at 20:45