【发布时间】:2018-03-25 17:03:44
【问题描述】:
我正在使用 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);可以使用但我没有任何成功。
感谢任何建议,谢谢
【问题讨论】:
-
解决方案在哪里实施?
-
我主要参考了以下文档:ngmap.github.io/#/!marker-remove.html,但没有使用聚类库删除标记/标记的示例。而且我正在以不同的方式构建我的标记。我正在使用这条线
var markerCluster = new MarkerClusterer($scope.assetMap, markers, mcOptions);来生成它们。$scope.assetMap.markers[key].setMap(null);返回错误:未定义标记。我在地图完成初始化后使用它作为测试来删除任何东西。你以前管理过吗?
标签: angularjs google-maps google-maps-api-3 ng-map