Search code examples
google-mapsgoogle-maps-api-3google-apigoogle-maps-markersgoogle-maps-api-2

removeOverlay in Google Maps API v3


How can I port this to v3? removeOverlay is not included in v3.

if( mapElements[lMapElementIndex]['marker'] != 0 ){
  //map.removeOverlay(mapElements[lMapElementIndex]['marker']); V2
}

V3?

if( mapElements[lMapElementIndex]['marker'] != 0 ){
  //map.removeOverlay(mapElements[lMapElementIndex]['marker']);
  mapElements(mapElements[lMapElementIndex]['marker']).setMap(null);
} //but throws an error mapElements is not a function

Solution

  • You need to use the setMap method of the overlay. From the docs:

    To remove an overlay from a map, call the overlay's setMap() method, passing null.

    Assuming mapElements is an array of objects, and that the marker property refers to an overlay instance, then all you need to do is:

    mapElements[lMapElementIndex]['marker'].setMap(null);
    

    Your attempt throws an error because you are trying to call mapElements as a function (when it appears to be an array). There is no need for that. Just get rid of the function call and it should work fine.