Javascript 删除一些标记而不清除所有标记markerClusterer.js
我有一个标记数组,并按类别过滤它们(可以打开/关闭类别)。Javascript 删除一些标记而不清除所有标记markerClusterer.js,javascript,angularjs,google-maps,google-maps-api-3,markerclusterer,Javascript,Angularjs,Google Maps,Google Maps Api 3,Markerclusterer,我有一个标记数组,并按类别过滤它们(可以打开/关闭类别)。 开始时,我开发了一个解决方案,只删除受影响的标记(过滤或删除)。不删除所有标记并重新绘制它们 当我得到markerClusterer.js时,很难找到它。因此,现在我清理所有标记并添加新标记。 有可能改进吗 $scope.clearMarkers = function(category){ // FOR 'markers' with 'category' // SET 'map'(null) && ADD
开始时,我开发了一个解决方案,只删除受影响的标记(过滤或删除)。不删除所有标记并重新绘制它们 当我得到markerClusterer.js时,很难找到它。因此,现在我清理所有标记并添加新标记。
有可能改进吗
$scope.clearMarkers = function(category){
// FOR 'markers' with 'category'
// SET 'map'(null) && ADD 'marker' to 'categoryPlaces'
// REMOVE 'markers' with 'category'
// CLEAR 'markerClusterer'
// CREATE new 'markerClusterer'
var categoryPlaces = [];
for (var i = 0; i < $scope.markers.length; i++) {
if($scope.markers[i].category == category){
$scope.markers[i].setMap(null);
categoryPlaces.push($scope.markers[i]);
}
}
for(var i = 0; i < categoryPlaces.length; i++){
$scope.markers.splice($scope.markers.indexOf(categoryPlaces[i]),1);
}
$scope.markerClusterer.clearMarkers();
$scope.markerClusterer = new MarkerClusterer($scope.map, $scope.markers, {
imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m'
});
}
$scope.toggleCategory = function(category) {
// IF 'category' is in 'categories' && 'categories'.length > 1
// REMOVE 'category' from 'categories'
// CLEAR 'markers' - REMOVE 'places' with 'category'
if($scope.categories.indexOf(category) != -1 && $scope.categories.length > 1){
$scope.categories.splice($scope.categories.indexOf(category),1);
$scope.clearMarkers(category);
}
// ELSE IF 'category' is not in 'categories'
// ADD 'category' to 'categories'
// ADD new 'places' with 'category'
// CLEAR 'markerClusterer'
// CREATE new 'markerClusterer'
else if($scope.categories.indexOf(category) == -1){
$scope.categories.push(category);
for (i = 0; i < places.length; i++) {
var newPlace = places[i];
if(newPlace.category == category){
$rootScope.$broadcast('map_setMarker', newPlace);
}
}
$scope.markerClusterer.clearMarkers();
$scope.markerClusterer = new MarkerClusterer($scope.map, $scope.markers, {
imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m'
});
}
// ELSE show 'mdToast'
else{
$mdToast.show(
$mdToast.simple()
.textContent('At least one category has to be active!')
.position('top right')
.hideDelay(3000)
);
}
}
$scope.clearMarkers=函数(类别){
//对于带有“类别”的“标记”
//设置“映射”(空)并将“标记”添加到“categoryPlaces”
//删除带有“类别”的“标记”
//清除“标记聚类器”
//创建新的“markerClusterer”
var类别位置=[];
对于(变量i=0;i<$scope.markers.length;i++){
if($scope.markers[i].category==category){
$scope.markers[i].setMap(null);
categoryPlaces.push($scope.markers[i]);
}
}
对于(变量i=0;i1
//从“类别”中删除“类别”
//清除“标记”-删除带有“类别”的“位置”
如果($scope.categories.indexOf(category)!=-1&&$scope.categories.length>1){
$scope.categories.splice($scope.categories.indexOf(category),1);
$scope.clearMarkers(类别);
}
//否则,如果“类别”不在“类别”中
//将“类别”添加到“类别”
//添加带有“类别”的新“地点”
//清除“标记聚类器”
//创建新的“markerClusterer”
else if($scope.categories.indexOf(category)=-1){
$scope.categories.push(类别);
对于(i=0;i