【问题标题】:Remove Google maps markers by click button again using JavaScript使用 JavaScript 再次单击按钮删除 Google 地图标记
【发布时间】:2014-01-24 13:04:43
【问题描述】:

我使用地理编码允许用户导航到特定地点,然后将特定半径内的标记添加到该导航点。如果用户再次单击按钮并想要导航到另一个地方,它应该删除第一个请求的所有标记。我不得不说导航点用蓝色标记标记,其他点用红色标记标记。我使用这段代码来做到这一点:

// global
var previousTarget = 0;
var markers = new Array(); // All my markers are stored here
// end global

geocoder.geocode(
    {'address': address},
    function(results, status){          
        if(status == google.maps.GeocoderStatus.OK){
            map.setCenter(results[0].geometry.location);
            marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: 'http://maps.google.com/mapfiles/ms/micons/blue.png'
            });

            var marker, i;
            var splitted = locations.splitted(",");

            for(var i = 0; i < splitted.length; i++){
                geocoder.geocode(
                    {'address': splitted[i]},
                    function(results, status){
                        if(results != null){
                            marker = new google.maps.Marker({
                                map: map,
                                position: results[0].geometry.location
                            });

                            markers.push(marker);
                            google.maps.event.addListener(marker, 'click', (function(marker, i){
                                return function() {
                                    $.ajax({
                                        url: 'myfile.php',
                                        type: 'POST',
                                        data: {addrData: jqXHR.responseText},
                                        success: function(datas, textStatus, jqXHR){
                                            for(var l = 0; l < markers.length; l++){
                                                if(marker == markers[l]){
                                                    infowindow.setContent('test');
                                                    infowindow.open(map, marker);
                                                }
                                            }
                                        },

                                        error: function(jqXHR, textStatus, errorThrown){
                                            alert("Error");
                                        }
                                    });
                                }
                            })(marker, i));
                        }
                    }
                );

                if(previousTarget > 1){ // Here is the function to click the button twice
                    markers.setMap(null);
                }
            }
        }
    }
);

如果我这样做,通过按两次按钮会在控制台中出现错误:

Uncaught TypeError: Object [object Array] has no method 'setMap'

如果我将markers 更改为marker,它只会删除蓝色标记。谁能给我一个提示,我在哪里犯了错误?

【问题讨论】:

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


【解决方案1】:

您需要遍历标记数组并删除每个标记。

for (var i=0; i<markers.length; i++) {

    markers[i].setMap(null);
}

【讨论】:

  • 刚刚意识到我可能应该把它作为评论,你为什么需要遍历每一个(不好笑,只是不确定我是否错过了一些东西)。
  • 因为每个marker 都是一个对象,您需要在其上调用setMap(null) 才能将其从地图中删除。这里markers只是一个存放这些对象的数组。
  • 谢谢,我做了一些研究,发现我在上面的 cmets 中发布的链接可以确定您的答案。打字前必须学会思考:)
  • 谢谢,工作几乎完美。它不会删除最后 2 个标记。你知道可能是什么原因吗?
  • 没有。编辑您的问题并添加您现在拥有的代码。或者更好的是,创建一个jsfiddle
猜你喜欢
  • 2012-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
相关资源
最近更新 更多