删除一些标记而不清除所有标记markerClusterer.js

时间:2017-08-10 21:07:49

标签: javascript angularjs google-maps google-maps-api-3 markerclusterer

我有一系列标记并按类别过滤它们(可以打开/关闭类别) 在开始时,我开发了一种解决方案,用于仅删除受影响的标记(过滤或删除)。不删除所有标记并重绘它们。

当我得到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)
      );
    }
}

0 个答案:

没有答案