Reputation: 1326
I am using this page to allow users to search for Points Of Interest within a given radius of an address that they type in.
The problem I'm having is that I can't clear the map of markers between a successful and then an unsuccessful search.
For example if I type in Swindon at a radius of 25 miles, 2 POI's are shown within the sidebar and their respective markers are shown on the map.
If I then use County Antrim as my search address again at 25 miles, the sidebar correctly shows 'No results found' but the markers from the initial serach are still on the map.
I've trawled the internet to try different pieces of code to get this to work but I'm really stuck with this issue. I've gone back to the source code on the Google site and I'm sure that I'm using the same code.
Could someone perhaps show me where I'm going wrong with this please?
Upvotes: 1
Views: 826
Reputation: 4473
You have a marker array defined but in your searchLocationsNear()
function you never add the markers to the array. So the array is empty when you try to loop through it in the clearLocations()
function.
Try this:
//...
for (var i = 0; i < markerNodes.length; i++) {
var sitedescription = markerNodes[i].getAttribute("sitedescription");
var distance = parseFloat(markerNodes[i].getAttribute("distance"));
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("siteosgb36lat")),
parseFloat(markerNodes[i].getAttribute("siteosgb36lon")));
var marker = createMarker(latlng, sitedescription);
markers.push(marker); //add marker to the markers array.
bounds.extend(latlng);
var sidebarEntry = createSidebarEntry(marker, sitedescription, distance);
sidebar.appendChild(sidebarEntry);
}
Upvotes: 0
Reputation: 114347
I use this:
for(var x=0;x<markers.length;x++) {
markers[x].setMap(null);
}
Upvotes: 3