如何从地图中清除前一个圆圈

时间:2017-08-08 09:36:43

标签: javascript google-maps google-maps-api-3

我需要在添加新圈子之前清除前一个圈子。 这是我的js代码的一部分

$(document).ready(function() {
    $('#select-distance').change(function() {
        var e = document.getElementById("select-distance");
        var value =  parseInt(e.options[e.selectedIndex].value);
        var circle = new google.maps.Circle({
             map: map,
             radius: value,
             fillColor: '#337ab7'
        });
        //circle.setMap(null);
        circle.bindTo('center', markerc, 'position');
        document.getElementById("search-button").addEventListener("click", find_closest_marker(value));  
    });
}); 

2 个答案:

答案 0 :(得分:0)

var circle; //circle global object
$(document).ready(function() {
if(typeof circle == "object") //check circle is present on map
{
 circle.setMap(null);
}
$('#select-distance').change(function() {
var e = document.getElementById("select-distance");
var value =  parseInt(e.options[e.selectedIndex].value);

circle = new google.maps.Circle({
map: map,
radius: value,
fillColor: '#337ab7'
});
//circle.setMap(null);
circle.bindTo('center', markerc, 'position');
document.getElementById("search-button").addEventListener("click", find_closest_marker(value));  

});
});

答案 1 :(得分:0)

重用圆圈变量。

String