Reputation: 3133
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
Upvotes: 1
Views: 2703
Reputation: 6057
The easiest way to keep only one Polyline for showDetails
on the map is to make a global Polyline variable. That way, each time showDetails
is called the global variable is modified.
Right now, each time showDetails
runs 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.
// GLOBAL
var detailsPolyline = new google.maps.Polyline({
strokeColor: "#FF0000",
strokeOpacity: 0.5,
strokeWeight: 5
});
inside showDetails
:
detailsPolyline.setPath(polylineCheckins);
detailsPolyline.setMap(map);
map.fitBounds(bounds);
Here's the whole test case I used, because I don't have the php file I created my own objects
var map;
var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP };
function initialize() {
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
showDetails([ {lat: 20, long: 0},
{lat: 20, long: 10},
{lat: 30, long: 20}]);
showDetails([ {lat: 10, long: 0},
{lat: 10, long: 10},
{lat: 20, long: 20}]);
}
var detailsPolyline = new google.maps.Polyline({
strokeColor: "#FF0000",
strokeOpacity: 0.5,
strokeWeight: 5
});
function showDetails(checkins)
{
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);
}
detailsPolyline.setPath(polylineCheckins);
detailsPolyline.setMap(map);
map.fitBounds(bounds);
}
Upvotes: 2
Reputation: 54618
You're defining the polyline
variable within the function itself, so once the function finishes, the variable is out of scope for any other methods (like setMap(null)
).
There are a couple of ways to do this. The simplist way is to define your polyline outside the function as a global variable:
var polyline = null;
function showDetails(itemId)
{
if (polyline != null)
{
polyline.setMap(null);
polyline = null;
}
/* more code */
polyline = new google.maps.Polyline({
path: polylineCheckins,
strokeColor: "#FF0000",
strokeOpacity: 0.5,
strokeWeight: 5
});
polyline.setMap(map);
map.fitBounds(bounds);
}
Upvotes: 1