Search code examples
javascriptarraysgoogle-mapsgoogle-maps-api-3push

Google map, remove control from controls array


I have a google map and I´m using a hacky solution for infowindows not to show up under a div that overlays the google map if a special user-interaction is done.

i execute this code after user-interaction :

var controlDiv = document.createElement('div');
controlDiv.style.width = '220px';
controlDiv.style.height = '500px';
map.controls[google.maps.ControlPosition.RIGHT_TOP].push(controlDiv);

that works wonderfully, but if the user closes the overlay i want to remove the element i pushed into the array.

i tryed to splice it out, but i have no idea how to select it, or what it´s index is.

map.controls[google.maps.ControlPosition.RIGHT_TOP].splice("?", 1);

other possibility may be

delete map.controls[google.maps.ControlPosition.RIGHT_TOP]

but i just dont get the element removed plesea help thanks in advance


Solution

  • map.controls[google.maps.ControlPosition.RIGHT_TOP] isn't a native array, it's a google.maps.MVCArray

    When you add only this single custom control to the array you may simply call:

    map.controls[google.maps.ControlPosition.RIGHT_TOP].clear();
    

    When there are more custom controls iterate over the controls to find the index of controlDiv and call

    map.controls[google.maps.ControlPosition.RIGHT_TOP].removeAt(index);
    

    Another approach: instead of adding/removing the control add the control once and toggle the display of controlDiv.