Brk Brk - 4 days ago 6
Javascript Question

Remove KMZ Layer from google maps

Hello I am trying to remove kmz layer from google maps.

I have the function displayLayer, which set the map to null if param is false

then I set the map to current google map otherwise I set the map to null.

When I am setting the map to null, it doesn't disappear but rather stays.

I am trying to understand this behavior but I don't find the right reason for that behavior.

function displayLayer(isDisplayLayer) {
var kmzLayer = new google.maps.KmlLayer('https://docs.google.com/uc?export=download&id=0B6SG3MFSYw7dSk1ma3B3akVzdFk', {
suppressInfoWindows: true,
preserveViewport: false,
});
if (isDisplayLayer) {
alert('set map to gmap');
kmzLayer.setMap(gmap);
} else {
alert('set map to null');
kmzLayer.setMap(null);
}
}

Answer

You are setting the map of a new KmlLayer that you are creating every time that the displayLayer function is called. You can put your kmzLayer creation for example on your initMap:

var map;
var kmzLayer;

function displayLayer(isDisplayLayer) {
    if (isDisplayLayer) {
        alert('set map to gmap');
        kmzLayer.setMap(map);
    } else {
        alert('set map to null');
        kmzLayer.setMap(null);
    }
}

function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 11,
        center: {lat: -36.90927416, lng: 174.84809875},
        streetViewControl: false,
        mapTypeControl: false,
        scaleControl: true,
        zoomControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    kmzLayer = new google.maps.KmlLayer('https://docs.google.com/uc?export=download&id=0B6SG3MFSYw7dSk1ma3B3akVzdFk', {
        suppressInfoWindows: true,
        preserveViewport: true,
    });
}
Comments