Кластер, MKMapView показывают много раз одну и ту же аннотацию - PullRequest
0 голосов
/ 27 апреля 2018

Итак, у меня есть приложение swift (swift 3), которое использует карту, на этой карте у меня много аннотаций, которые обновляются, когда пользователь меняет регион карты.

Я хотел использовать модуль «Кластер», добавляю его, и когда я увеличиваю масштаб, чтобы показать, что находится в кластере, аннотации появляются несколько раз почти в одном и том же месте (иногда это образует круг, см. Рисунок).

enter image description here

Я уже пытался отфильтровать свой список, чтобы получить только один раз аннотацию в кластере, но он не работает.

Этот метод вызывается каждый раз, когда пользователь меняет регион:

   func createAnnotations(pois list:[PlaceModel]) {
    poiAnnotations.removeAll()

    let filteredAnnotations = mapView.annotations.filter { annotation in
        //if annotation is MKUserLocation { return false }          // don't remove MKUserLocation

        if let temp = annotation.subtitle, let value = temp {
            return value == "Place"
        }
        return false
    }

    clusterManager.remove(poiAnnotations)
    clusterManager.remove(filteredAnnotations)

    for poi in list {
        let centerLocation = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
        if poi.getLocation().distance(from: centerLocation) > Double(Constants.POI_Radius) {
            continue
        }

        let annotation = Annotation()
        annotation.coordinate = CLLocationCoordinate2D(latitude: poi.latitude!, longitude: poi.longitude!)
        annotation.title = poi.name!
        annotation.subtitle = "Place"
        annotation.type =  .image( Images.MapPins.velhop)


        // For first value
        if poiAnnotations.isEmpty {
            poiAnnotations.append(annotation)
        }

        for pois in poiAnnotations {
            if poiAnnotations.contains(where: {$0.title == poi.name}) {
                // Do nothing
            } else {
                poiAnnotations.append(annotation)
            }
        }
    }    
    clusterManager.add(poiAnnotations)
}

А вот место, где создаются кластеры и аннотации:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? ClusterAnnotation {

        let identifier = "Cluster"
        var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        // Type for cluster
        let color = UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1)
        let type:ClusterAnnotationType = .color(color, radius: 25)

        if let view = view as? BorderedClusterAnnotationView {
            view.annotation = annotation
            view.configure(with: type)
        } else {
            view = BorderedClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: type, borderColor: .white)
        }
        return view
    } else {

        let identifier = "Pin"
        var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

        if let view = view {
            view.annotation = annotation      
        } else {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        }

        return view
    }
}

Я пробовал несколько вещей, которые не удалось .. Заранее спасибо.

...