I have this function that after a click on a marker draws a line between points that are related to an item.
function showDetails(itemId)
{
var newlatlng = itemId.position;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "index.php?r=items/ajaxdetails&id="+itemId.indice,
false);
xmlhttp.send();
var checkins = JSON.parse(xmlhttp.responseText);
var numeroCheckins = checkins.length;
var polylineCheckins = [];
var bounds = new google.maps.LatLngBounds();
for (counter = 0; counter< numeroCheckins; counter++)
{
var posizione = new google.maps.LatLng(checkins[counter].lat,
checkins[counter].long);
polylineCheckins[counter] = posizione;
bounds.extend(posizione);
}
var polyline = new google.maps.Polyline({
path: polylineCheckins,
strokeColor: "#FF0000",
strokeOpacity: 0.5,
strokeWeight: 5
});
polyline.setMap(map);
map.fitBounds(bounds);
}
Everything is working, but if a call several time this function the previous line is always showed. I tried to use the method setMap(null) but without success, to try to reset the polyline.
I would like to achieve te result of deleting the previous polyline, before drawing a new one.
Thanks for your support
The easiest way to keep only one Polyline for
showDetailson the map is to make a global Polyline variable. That way, each timeshowDetailsis called the global variable is modified.Right now, each time
showDetailsruns a new Polyline is created and no reference to it is returned, so I don’t see a way to set the previous line’s map to null.inside
showDetails:Here’s the whole test case I used, because I don’t have the php file I created my own objects