我创建和聚类我与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);
可以使用但我没有任何成功。
任何建议表示赞赏,感谢
你在哪里实现你所看到的('markers [key] .setMap(null);')是解决方案吗? – MrUpsidown
我一直主要参考这个文档: https://ngmap.github.io/#/!marker-remove.html 但是没有使用聚类库去除标记/标记的例子。我正在以不同的方式构建我的标记。我正在使用此行 'var markerCluster = new MarkerClusterer($ scope.assetMap,markers,mcOptions);' 生成它们。 '$ scope.assetMap.markers [key] .setMap(null);'返回错误:marker未定义。在地图完成初始化之后,我正在使用它作为测试来删除任何内容。你之前是否管理过它? – Smiter