我正在使用markerclusterer库和此代码创建和聚类我的标记:
var response = _client.Search<IndexBase>(s => s
.Type(Types.Type(typeof(A), typeof(B)))
.Query(qry => qry
.Bool(b => b
.Must(m => m
.QueryString(qs => qs
.DefaultField("_all")
.Query(request.Query)))))
.Highlight(h =>
h.Fields(f => f.Field("_all")))
这在第一次迭代时工作正常。
再次使用新位置点击populateMapLocationData函数,边界发生变化,地图居中并缩放到新标记的新位置,所以我认为它正在运行,但所有以前的标记仍在那里。
我想要实现的是当我使用一组新位置调用populateMapLocationData时,清除现有标记并使用新标记更新地图。
我见过 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;
};
可以使用,但我没有取得任何成功。
感谢任何建议,谢谢
答案 0 :(得分:1)
实际上,如果您使用的是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
我为您提供了一个更好的示例,其中我使用了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>