我有一个简单的地图视图:
@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)
}
但根本没有调用。为什么呢?
答案 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 {
答案 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")
}
}
}