删除多个圈子google maps api

时间:2014-10-28 11:45:53

标签: javascript google-maps-api-3

我试图用“zoom_change”事件删除圆圈。它在放大时会创建它们,但在缩小时不会删除它们。另一方面,标记效果很好,它们在放大时出现,在缩小时消失。任何人都可以帮助我吗?

        function plotBusinesses() {
            var i;
            for (i = 0; i < businesses.length; i++) {
                codeBusinesses(businesses[i]);
            }
        }

        function codeBusinesses(address) {
            geocoder.geocode({'address': address[1]}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location,
                        icon: image,
                        title: address[0]
                    });

                    //avoiding preloading markers
                    marker.setMap(null);

                    var covered = {
                        strokeColor: '#FF0000',
                        strokeOpacity: 1,
                        strokeWeight: 0,
                        fillColor: 'green',
                        fillOpacity: 0.8,
                        //map: map,
                        center: results[0].geometry.location,
                        radius: 80
                    };


                    circleBuss = new google.maps.Circle(covered);
                    circleBuss.setMap(null);

                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(address[0]);
                        infowindow.open(map, this);
                    });
                    bounds.extend(results[0].geometry.location);

                    google.maps.event.addListener(map, 'zoom_changed', function() {
                        var zoom = map.getZoom();
                        //alert(zoom);

                        if (zoom <= 12) {
                            for (var i = 0; i < markersArray.length; i++) {
                                markersArray[i].setMap(null);
                                circleBuss[i] = new google.maps.Circle(covered);
                                circleBuss[i].setMap(null);               
                            }

                        } else {
                            //marker.setMap(map);
                            for (var i = 0; i < markersArray.length; i++) {
                                markersArray[i].setMap(map);
                                circleBuss[i] = new google.maps.Circle(covered);
                                circleBuss[i].setMap(map);
                            }
                        }
                    });
                    markersArray.push(marker);
                }
                else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
                //map.fitBounds(bounds);
            });
        }

1 个答案:

答案 0 :(得分:2)

创建一个圆形数组并像对标记一样迭代它。

像这样的东西

var Circles = [];

function plotBusinesses() {
    var i;
    for (i = 0; i < businesses.length; i++) {
        codeBusinesses(businesses[i]);
    }
}

function codeBusinesses(address) {
    geocoder.geocode({'address': address[1]}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: image,
                title: address[0]
            });

            //avoiding preloading markers
            marker.setMap(null);

            var covered = {
                strokeColor: '#FF0000',
                strokeOpacity: 1,
                strokeWeight: 0,
                fillColor: 'green',
                fillOpacity: 0.8,
                //map: map,
                center: results[0].geometry.location,
                radius: 80
            };

            Circles.push(new google.maps.Circle(covered));

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(address[0]);
                infowindow.open(map, this);
            });
            bounds.extend(results[0].geometry.location);

            google.maps.event.addListener(map, 'zoom_changed', function() {
                var zoom = map.getZoom();
                //alert(zoom);

                if (zoom <= 12) {
                    for (var i = 0; i < markersArray.length; i++) {
                        markersArray[i].setMap(null);           
                    }
                    for (var i = 0; i < Circles.length; i++) {
                        Circles[i].setMap(null);
                    }
                } else {
                    //marker.setMap(map);
                    for (var i = 0; i < markersArray.length; i++) {
                        markersArray[i].setMap(map);
                    }
                    for (var i = 0; i < Circles.length; i++) {
                        Circles[i].setMap(map);
                    }
                }
            });
            markersArray.push(marker);
        }
        else {
            alert("Geocode was not successful for the following reason: " + status);
        }
        //map.fitBounds(bounds);
    });
}