MapKit显示注释群集以及非群集注释

时间:2016-06-10 11:41:11

标签: swift mapkit mkannotation mkannotationview markerclusterer

我是iOS开发新手,目前正在使用FBAnnotationClusteringSwift集群制造商。我的应用需要有两个不会聚集的注释,因为它们表示sourcedestination地址。剩余的注释必须在它们代表stations时进行聚类。

我想要实现的目标就像下面的图像,其中" A"是我的源地址,群集代表电台:

That is my goal

然而,正在发生的是,创建群集时,表示非群集注释的注释(源地址)消失,如下所示:

Real

如果图书馆将所有注释混合在一起,那就非常糟糕了,应该有办法将我通过clusteredAnnotationsWithinMapRect(电台)添加的那些和之前添加的标记分开(地址)。以下是当前代码的相关部分:

var markerClusterer: FBClusteringManager?

ViewController ... {

  override func viewDidLoad() {
    super.viewDidLoad()
    ...
    self.markerClusterer = FBClusteringManager()
    self.markerClusterer!.delegate = self
  }

  func cellSizeFactorForCoordinator(coordinator:FBClusteringManager) -> CGFloat{
    return 1.0
  }

  func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
      // Here is just like the example code from FBAnnotationClusterSwift
      var stations: [MKAnnotation] = fetchStations(...)
      if (stations.count > 0) {
          NSOperationQueue().addOperationWithBlock({
              let scale:Double = Double(self.mapView.bounds.size.width) / self.mapView.visibleMapRect.size.width
              self.markerClusterer!.addAnnotations(stations)
              var annotationArray = stations
              // Needed to do that otherwise the clusters never "explode" into pins
              if (scale <= 0.06) {
                  annotationArray = self.markerClusterer!.clusteredAnnotationsWithinMapRect(self.mapView.visibleMapRect, withZoomScale:scale)
              }
              self.markerClusterer!.displayAnnotations(annotationArray, onMapView: self.mapView)
            })
        }              
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        var reuseId = ""

        if (annotation.isKindOfClass(FBAnnotationCluster)) {
            reuseId = "Cluster"
            var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
            if clusterView == nil {
                clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, options: nil)
            } else {
                clusterView!.annotation = annotation
            }
            return clusterView
        } else if (annotation.isKindOfClass(AddressPointAnnotation)) {
            reuseId = "AddressPin"
            var addressPinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
            if addressPinView == nil {
                addressPinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                addressPinView!.canShowCallout = true
            }
            else {
                addressPinView!.annotation = annotation
            }
            let addressPin = annotation as! AddressPointAnnotation
            addressPinView!.image = UIImage(named: addressPin.icon)
            return addressPinView
        } else if (annotation is Station) {
            reuseId = "StationPin"
            var stationPinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
            if stationPinView == nil {
                stationPinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            }
            else {
                stationPinView!.annotation = annotation
            }
            let stationPin = annotation as! Station
            stationPinView!.image = UIImage(named: "station")
            return stationPinView
        } else {
            return nil
        }
    }

}

// Annotation for the stations
class Station: NSObject, MKAnnotation {
   var id: Int = 0
   var availableBikes: Int = 0
   var availableBikeStands: Int = 0
   var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 39.208407, longitude: -76.799555)
}

// Annotations for addresses
class AddressPointAnnotation: MKPointAnnotation {
  var icon: String!
}

class Address: NSObject {

    var marker: AddressPointAnnotation?

    func addMarker(coordinate: CLLocationCoordinate2D) {
        marker = AddressPointAnnotation()
        marker!.coordinate = coordinate
        // ViewController is passed to the Address, so it can add itself to the map
        self.controller.mapView.addAnnotation(marker!)
        if (self.direction == SOURCE) {
            marker!.title = "Starting address"
            marker!.icon = "from"
        } else {
            marker!.title = "Destination address"
            marker!.icon = "to"
        }
        self.controller.mapView.addAnnotation(marker!)
    }
}

任何帮助,想法或评论都非常受欢迎。感谢。

0 个答案:

没有答案