Question

I have an application that contains an embedded browser window.

I want to have my application create a kmlLayer. I can do this on the fly with the following:

function addKmlLayer(kmlURL) {
    var kmlLayer = new google.maps.KmlLayer({ url: kmlURL });
    kmlLayer.setMap(my_map);
}

I'm going to allow my users to add several kmlLayers.

I also know that I can remove the layer by using kmlLayer.setMap(null).

How do I find my previous kmlLayer to set it to null? Is there a way to identify the kmlLayer (ID, Name, etc...)?

Was it helpful?

Solution

One option is to keep a global reference to the KmlLayer

var kmlLayer = null;
function addKmlLayer(kmlURL) {
    kmlLayer = new google.maps.KmlLayer({ url: kmlURL });
    kmlLayer.setMap(my_map);
}

Then to hide it, this will work:

kmlLayer.setMap(null); 

Might want to do:

if ((kmlLayer != null) && kmlLayer.setMap) kmlLayer.setMap(null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top