【问题标题】:How to clear previous circle from map如何从地图中清除上一个圆圈
【发布时间】:2017-08-08 09:36:42
【问题描述】:

我需要先清除上一个圆圈,然后再添加一个新圆圈。 这是我的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));  
    });
}); 

【问题讨论】:

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


【解决方案1】:

重复使用圆形变量。

var circle = new google.maps.Circle({
                        map: map,
                        radius: // PUT HERE INITIAL VALUE OR 0,
                        fillColor: '#337ab7'
                    });
$(document).ready(function() {                      
                   $('#select-distance').change(function() {
                    var e = document.getElementById("select-distance");
                    var value =  parseInt(e.options[e.selectedIndex].value);

                    circle.setRadius(value);
                    circle.bindTo('center', markerc, 'position');
                    document.getElementById("search-button").addEventListener("click", find_closest_marker(value));  

                  });
                }); 

【讨论】:

    【解决方案2】:
    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));  
    
    });
    });
    

    【讨论】:

    • ReferenceError: 圆圈未定义!!
    • @KARAN LAGALWAR 我检查了很多答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多