2017-10-13 20 views
0

我創建和聚類我與markerclusterer庫標記和該代碼刪除谷歌地圖標記:我如何刷新/使用ngMaps

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);可以使用但我沒有任何成功。

任何建議表示讚賞,感謝

+0

你在哪裏實現你所看到的('markers [key] .setMap(null);')是解決方案嗎? – MrUpsidown

+0

我一直主要參考這個文檔: https://ngmap.github.io/#/!marker-remove.html 但是沒有使用聚類庫去除標記/標記的例子。我正在以不同的方式構建我的標記。我正在使用此行 'var markerCluster = new MarkerClusterer($ scope.assetMap,markers,mcOptions);' 生成它們。 '$ scope.assetMap.markers [key] .setMap(null);'返回錯誤:marker未定義。在地圖完成初始化之後,我正在使用它作爲測試來刪除任何內容。你之前是否管理過它? – Smiter

回答

3

其實,如果你使用的是谷歌的原markerclusterer.js,除去你只需要使用它的MarkerClusterer.prototype.removeMarker功能,並刪除標記的數組,你只需要使用它的MarkerClusterer.prototype.removeMarkers幸運的標誌, ngMaps的markerclusterer.js只是原始的包裝。 更多關於在谷歌的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>