removing a KMLlayer

2,141 views
Skip to first unread message

Jennifer Dudeck

unread,
Oct 11, 2010, 10:54:25 AM10/11/10
to Google Maps JavaScript API v3

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 });
}
}

geoco...@gmail.com

unread,
Oct 11, 2010, 11:26:05 AM10/11/10
to Google Maps JavaScript API v3
On Oct 11, 7:54 am, Jennifer Dudeck <jdudec...@gmail.com> wrote:
> 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?

have you tried soils.setMap(null); to remove it?

from the documentation:
http://code.google.com/apis/maps/documentation/javascript/reference.html#KmlLayer
setMap(map:Map) None Renders the KML Layer on the specified map. If
map is set to null, the layer is removed.

If that doesn't help, a link might get more assistance.

-- Larry

John M Phillips

unread,
Oct 11, 2010, 9:24:58 PM10/11/10
to Google Maps JavaScript API v3
Since "soils" is created with a new command, you need to delete the
old value
before creating a new object.

There may be better coding, such as testing for existance of soils,
but here is
what I have done in a similar situation

1) make soils global in your script in the outer most block with
var soils;

2) force a known creation of soils, note do not declare another
variable;
soils = new google.maps.KmlLayer( ...

3) to put new kml data
soils.setMap(null); // remove from
display;
delete soils; // delete
the objects data
soils = new google.maps.KmlLayer( ... // add new data to the
object

On Oct 11, 11:26 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Oct 11, 7:54 am, Jennifer Dudeck <jdudec...@gmail.com> wrote:
>
> > 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?
>
> have you tried soils.setMap(null); to remove it?
>
> from the documentation:http://code.google.com/apis/maps/documentation/javascript/reference.h...

Nathan Raley

unread,
Oct 12, 2010, 9:04:14 AM10/12/10
to google-map...@googlegroups.com
Did you try storing it in a global variable.  Before you fetch the new kml, use the .setMap for the old KML file to Null.  Now store the new kml into the old variable, then redisplay?


--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Jennifer Dudeck

unread,
Oct 13, 2010, 4:53:53 AM10/13/10
to Google Maps JavaScript API v3
Thank you for your replies! I was able to get it to work by doing
this:

First, I declared "var soils;" at the top of my document.
Then, in my initialize function, I gave soils a value before I put my
event listener:

soils = new google.maps.KmlLayer(null);
google.maps.event.addListener(map, 'idle', showSoils);

Finally, I used setMap(null) at the beginning of my function to remove
whatever the current soils overlay was. Now it works!

New form of my function:

function showSoils() {

//remove previous overlay
soils.setMap(null);

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 });
}
}


Hopefully this will be helpful to someone else with the same question.
Reply all
Reply to author
Forward
0 new messages