我如何使用 ngMaps 在 Google 地图上 refresh/delete 标记

How can I refresh/delete markers on Google maps using ngMaps

我正在使用 markerclusterer 库和以下代码创建和聚类我的标记:

    function populateMapLocationData(locations) {

        NgMap.getMap({id:'map'}).then(function(map) {
            $scope.assetMap = map;
            $scope.initMarkerClusterer(locations);
        });

    };

    $scope.initMarkerClusterer = function(locations) {

        $scope.bounds = new google.maps.LatLngBounds();
        var markers = $scope.createMarker(locations);

        var mcOptions = { imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m' };
        var markerCluster = new MarkerClusterer($scope.assetMap, markers, mcOptions);

        $scope.assetMap.fitBounds($scope.bounds);
        $scope.assetMap.panToBounds($scope.bounds);

    };

    $scope.createMarker = function(location) {
        var latLng = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lang));
        $scope.bounds.extend(latLng);
        var marker = new google.maps.Marker({position: latLng, title: asset.name});
        google.maps.event.addListener(marker, 'click', function() {

            var infowindow = new google.maps.InfoWindow();
            var center = new google.maps.LatLng( parseFloat(asset.lat), parseFloat(asset.lang) );

            infowindow.setContent("content");
            infowindow.setPosition(center);
            infowindow.open($scope.assetMap);

            google.maps.event.addListener($scope.assetMap, 'click', function(event) {
                infowindow.close();
            });

        });

        return marker;
    };

这在第一次迭代时运行良好。

再次使用新位置点击 populateMapLocationData 函数,边界发生变化,地图居中并缩放到新标记的新位置,所以我认为它正在工作,但所有以前的标记仍然存在。

我想要实现的是,当我使用一组新位置调用 populateMapLocationData 时,清除现有标记并使用新标记更新地图。

我看到markers[key].setMap(null);可以用了,但是没成功

感谢任何建议,谢谢

实际上,如果您使用的是 Google 的原始 markerclusterer.js,要删除一个标记,您只需要使用它的 MarkerClusterer.prototype.removeMarker 函数,而要删除一个标记数组,您只需使用它的 MarkerClusterer.prototype.removeMarkers 幸运的是,ngMaps 的 markerclusterer.js 只是原始的包装器。 Google 的 documentation

中有更多相关信息

例如:

vm.dynMarkers = [ {marker1}, {marker2}, ...] // your marker array
vm.markerClusterer = new MarkerClusterer(map, vm.dynMarkers, {}); // creater your marker cluster
vm.markerClusterer.removeMarkers(vm.dynMarkers); // remove all markers

我为您制作了一个 plunker 示例,其中我使用 ngMaps 示例库作为基础,这样对您来说更容易(确保使用您自己的 API 密钥):https://plnkr.co/edit/RZNc2KLdO8qmW0o2Kimq?p=preview

这也是嵌入式代码:

var app = angular.module('myApp', ['ngMap']);

app.controller('mapController', function($http, $interval, NgMap) {
  var vm = this;
  vm.removeAllMarkers = removeAllMarkers;
  vm.dynMarkers = [];
  vm.map;

  NgMap.getMap().then(function(map) {
    for (var i = 0; i < 1000; i++) {
      var latLng = new google.maps.LatLng(markers[i].position[0], markers[i].position[1]);
      vm.dynMarkers.push(new google.maps.Marker({
        position: latLng
      }));
    }
    vm.markerClusterer = new MarkerClusterer(map, vm.dynMarkers, {});
    vm.map = map;
  });

  function removeAllMarkers() {
    vm.markerClusterer.removeMarkers(vm.dynMarkers);
    vm.dynMarkers = [];
    console.log('all markers in cluster removed');
  }
});
map,
div[map] {
  display: block;
  width: 600px;
  height: 400px;
}
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <title>Dynamic ngMap demo</title>
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  <script src="https://maps.google.com/maps/api/js?key=AIzaSyCD0rWgXRVBa7PgaTWXVp_gU51pgHGviYY"></script>
  <script src="https://code.angularjs.org/1.3.15/angular.js"></script>
  <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
  <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/testapp/scripts/markerclusterer.js"></script>
  <script>
    MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ = 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m'; //changed image path
  </script>
  <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/testapp/scripts/markers.js"></script>
</head>

<body>
  <h1>Marker Cluster</h1>
  <hr />
  <div ng-controller="mapController as vm">
    <ng-map zoom="2" center="[43.6650000, -79.4103000]"></ng-map>
    <button ng-click="vm.removeAllMarkers();" type="button">Remove All Markers</button>
  </div>
</body>

</html>