Remove Circle / Polygons from Google Map api v3

What differentiates circle/rectangle from a polygon in Google maps is that circle/rectangles are interpreted as closed figures bounded by bounds where as for a polygon you can define a path with number of points on it to draw. Although they both can be removed by setting map as null, Polygons still offer more options. If whole figure is to be removed, call setMap with null. But if only parts of polygon has to be removed, iterate over them and remove them using removeAt. Following example shows
<script>
/* 
** closedFig is the object of class google.maps.Circle or google.maps.Rectangle or 
** google.maps.Polygon
*/
removeFig(closedFig) {
closedFig.setMap(null);
}

/* 
** poly is the object of class google.maps.Polygon
** vertices is the collection of vertices to be removed. 
** if vertices empty this removes the whole polygon
*/
removeSelected(poly, vertices) {
       if (vertices.length == 0 )
		vertices = poly.getPath();

	// Iterate over the vertices.
	for (var i = vertices.length-1; i > -1; i--) {
		vertices.removeAt(i);
	}
}
</script>
  • John

    is there a way to clear a polygon with a button event:
    function clearShape() {
    shape.setMap(null);
    }

    It works however when attempting to reclick on the map it doesn’t allow for adding a new polygon…any ideas? Thanks
    John

    • http://enbake.com arpan

      John,

      The following article clears and redraws circle on map using same solution.
      http://enbake.com/draw-circle-with-google-maps-api-v3

      if this doesnt help show us your code and usage and then we can look into it better.

      Thanks.

    • http://twitter.com/enbake Enbake Consulting

      John,

      The following article clears and redraws circle on map using same solution.
      http://enbake.com/draw-circle-

      if this doesnt help show us your code and usage and then we can look into it better.

      Thanks.

  • John

    is there a way to clear a polygon with a button event:
    function clearShape() {
    shape.setMap(null);
    }

    It works however when attempting to reclick on the map it doesn’t allow for adding a new polygon…any ideas? Thanks
    John

    • Anonymous

      John,

      The following article clears and redraws circle on map using same solution.
      http://enbake.com/draw-circle-

      if this doesnt help show us your code and usage and then we can look into it better.

      Thanks.

  • http://twitter.com/enbake Enbake Consulting

    John,

    The following article clears and redraws circle on map using same solution.
    http://enbake.com/draw-circle-with-google-maps-api-v3

    if this doesnt help show us your code and usage and then we can look into it better.

    Thanks.

  • Anonymous

    John,

    The following article clears and redraws circle on map using same solution.
    http://enbake.com/draw-circle-with-google-maps-api-v3

    if this doesnt help show us your code and usage and then we can look into it better.

    Thanks.

  • Sini

    you’ve still got this article somewhere? http://enbake.com/draw-circle-with-google-maps-api-v3 it’s not online anymore. would be great because i found it last week for using it this week but it’s offline now. Thanks a lot

  • Qasim

    This Link You’ve posted is offline. Can you bring look into it ?