最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Map: Remove all Circles - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Oct 11, 2016 at 21:02 LaurenLauren 7853 gold badges13 silver badges30 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

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>

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 = [];
发布评论

评论列表(0)

  1. 暂无评论