【问题标题】:AngularJS & Google Map API V3 remove marker from mapAngularJS 和 Google Map API V3 从地图中删除标记
【发布时间】:2026-01-27 06:30:02
【问题描述】:

我脑子里乱七八糟,不知道为什么这不起作用

var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
geocode_results[i]['lat'] = lat;
geocode_results[i]['lng'] = lng;
geocode_results[i]['l_type'] = top_location.geometry.location_type;
marker = new google.maps.Marker({
    icon: mapIcon,
    position: new google.maps.LatLng(lat,lng),
    map: map
});
markersArray.push(top_location.address_components[0].long_name);

使用上述方法创建了我的标记并将它们绘制在我的地图上。

使用以下代码从地图中删除标记​​p>

$scope.removemarkers = function() {
    console.log($scope);
    console.log(markersArray);
    if (markersArray && markersArray.length) {
        for (var i = 0; i < markersArray.length; i++) {
            markersArray[i].setMap(null);
        }
        markersArray = [];
    }
};

我在console.log() 中收到以下错误

TypeError: Object AB42 2DL has no method 'setMap'at Object.$scope.removemarkers

AB42 2DL 是绘制标记时使用的随机邮政编码

【问题讨论】:

  • 您必须将marker 推送到markersArray。然后标记将使用setMap(null) 从地图中删除

标签: angularjs google-maps-api-3


【解决方案1】:

markersArray 不包含标记,它包含字符串。

试试这个:

markersArray.push(marker);

【讨论】: