I have polygons, representing different soil types, stored in a
database. What I am attempting to accomplish is to show only the
polygons currently in the viewing window. When the bounds or zoom is
changed, a function is enacted that gets the KML for a new KMLlayer
from a PHP script. All of this works just fine, but there is one
hitch. I want to remove the previous KMLlayer every time the bounds/
zoom is changed, and replace it with the new KML. However, right now,
it is not removing the old KML, just adding the new polygons on top of
it. Can anyone help me figure out why my code is not removing the old
KML?
This is the event listener:
google.maps.event.addListener(map, 'idle', showSoils);
And this is the actual function:
function showSoils() {
//remove previous overlay
var soils = new google.maps.KmlLayer();
var zoomlevel = map.getZoom();
//if the zoom level is close enough
if(zoomlevel>13)
{
//obtain points in database-friendly format:
var bounds = map.getBounds();
var ne = String(bounds.getNorthEast());
var firstcomma = ne.search("[,]");
firstlat = ne.substring(1, firstcomma);
firstlong = ne.substring(firstcomma+2, ne.length-1);
var sw = String(bounds.getSouthWest());
var firstcomma = sw.search("[,]");
secondlat = sw.substring(1, firstcomma);
secondlong = sw.substring(firstcomma+2, sw.length-1);
bcoords = firstlat + " " + firstlong + ", " + secondlat + " " +
firstlong + ", " + secondlat + " " + secondlong + ", " + firstlat + "
" + secondlong + ", " + firstlat + " " + firstlong;
var soils = new google.maps.KmlLayer('
http://myfarms.org/test/
soils_in_view.php?bcoords=' + escape(bcoords), { map: map,
preserveViewport: true });
}
}