2

I'm looking for a javascript function that will clear all drawings from my map; something like map.removeMarkers() or map.removeOverlays(), but for shapes - specifically circles.

I've seen some answers about how to do this on Android, but I'm looking for a web solution. I'm using gmaps.js to draw my circles:

// create circle loop
for( i = 0; i < data.mapArray.length; i++ ) {

    circle = map.drawCircle({
        lat: data.mapArray[i].lat,
        lng: data.mapArray[i].lng,
        radius: parseInt(data.mapArray[i].radius),
        strokeColor: '#'+data.mapArray[i].color,
        strokeWeight: 8,
        fillOpacity: 0,

        click: (function (e) {
            return function () {
                $('#'+modalType).modal({
                    remote: modalURL+e
                });
            };
        })(data.mapArray[i].id)
    });

} // end loop

I'm guessing that within this loop I need to add the circles to an array, and then call a function to clear all of them, but I'm not sure how to execute that.

Lauren
  • 743
  • 3
  • 12
  • 24

2 Answers2

6

One easy solution is to store the objects in an array

<input type="button" value="Clear all" onclick="removeAllcircles()"/>
<script>
var circles = [];
// create circle loop
for( i = 0; i < data.mapArray.length; i++ ) {
    var circle = map.drawCircle({
        lat: data.mapArray[i].lat,
        lng: data.mapArray[i].lng,
        radius: parseInt(data.mapArray[i].radius),
        strokeColor: '#'+data.mapArray[i].color,
        strokeWeight: 8,
        fillOpacity: 0,
        click: (function (e) {
            return function () {
                $('#'+modalType).modal({
                    remote: modalURL+e
                });
            };
        })(data.mapArray[i].id)
    });
    // push the circle object to the array
    circles.push(circle);
} // end loop
// remove All circles
function removeAllcircles() {
  for(var i in circles) {
    circles[i].setMap(null);
  }
  circles = []; // this is if you really want to remove them, so you reset the variable.
}
</script>

EDIT

Once you have that array, you can use it to toggle on/of, or target some specific circle, like circkles[17] ...

<input type="button" value="Toggle on" onclick="toggleOn()"/>
<input type="button" value="Toggle off" onclick="toggleOff()"/>
<script>
// Toggle off
function toggleOff() {
  for(var i in circles) {
    circles[i].setMap(null);
  }
}
// Toggle on
function toggleOn() {
  for(var i in circles) {
    circles[i].setMap(map);
  }
}
</script>
Emmanuel Delay
  • 3,619
  • 1
  • 11
  • 17
0

Think this code is easier bit.

var circles = [];
// create circle loop
for( i = 0; i < data.mapArray.length; i++ ) {

    circle = map.drawCircle({
        lat: data.mapArray[i].lat,
        lng: data.mapArray[i].lng,
        radius: parseInt(data.mapArray[i].radius),
        strokeColor: '#'+data.mapArray[i].color,
        strokeWeight: 8,
        fillOpacity: 0,

        click: (function (e) {
            return function () {
                $('#'+modalType).modal({
                    remote: modalURL+e
                });
            };
        })(data.mapArray[i].id)
    });
    circles.push(circle);

} // end loop

and when need to remove just do..

circles.forEach((circle) => {
    circle.setMap(null);
});
circles = [];
vimuth
  • 5,064
  • 33
  • 79
  • 116