为什么不调用MKMapView中的clusterAnnotationForMemberAnnotations?

时间:2017-12-20 21:56:30

标签: ios swift mapkit

我有一个简单的地图视图:

@IBOutlet private var mapView: MKMapView!

然后逐一添加注释:

mapView.addAnnotation(Annotation(user: user))

并向他们展示:

mapView.showAnnotations(mapView.annotations, animated: true)

我也实现了这个方法:

func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
    print(memberAnnotations)
    return MKClusterAnnotation(memberAnnotations: memberAnnotations)
}

但根本没有调用。为什么呢?

enter image description here enter image description here

3 个答案:

答案 0 :(得分:8)

似乎需要为MKAnnotationView设置clusteringIdentifier。这对我有用:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation)
    annotationView.clusteringIdentifier = "identifier"
    return annotationView
}

答案 1 :(得分:0)

检查

Function interpolateLookup(lookupVal As Range, lookupRange As Range, colID As Integer) As Double
     step = 0.01

     x0 = Application.Round(lookupVal.Value, 2)
     y0 = Application.VLookup(x0, lookupRange, colID, False)

     x1 = Application.Round(lookupVal.Value, 2) + step
     y1 = Application.VLookup(x1, lookupRange, colID, False)

     x = lookupVal.Value

     interpolateLookup = Application.Round(weightedAverage(x0, y0, x1, y1, x), 1)
End Function

class YourViewController: MKMapViewDelegate {

另请参阅:https://github.com/artemnovichkov/iOS-11-by-Examples/blob/master/iOS-11-by-Examples/MapKit/MapKitViewController.swift

答案 2 :(得分:0)

@ peter的答案对我有用,但我发现直接在MKAnnotationView上设置clusterIdentifier而不是使用委托方法来设置它更直观。例如:

class VehicleAnnotationView: MKAnnotationView {
    override var annotation: MKAnnotation? {
        willSet {
            clusteringIdentifier = "1"

            canShowCallout = true
            calloutOffset = CGPoint(x: -5, y: 5)
            rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

            image = UIImage(named: "MapVehiclePin")

        }
    }
}