Swift 群集,MKMapView显示多次相同的注释
所以,我有一个使用地图的swift应用程序(swift 3),在这个地图中,我有许多注释,当用户更改地图区域时,这些注释会更新 我想使用pod“Cluster”,我添加了它,当我放大显示集群中的内容时,注释几乎在同一个位置出现了好几次(有时它会形成一个圆圈,见图) 我已经尝试过过滤我的列表,以便在集群中只获得一个注释实例,但它不起作用 每次用户更改区域时都会调用此方法:Swift 群集,MKMapView显示多次相同的注释,swift,mkmapview,mkannotation,mkannotationview,Swift,Mkmapview,Mkannotation,Mkannotationview,所以,我有一个使用地图的swift应用程序(swift 3),在这个地图中,我有许多注释,当用户更改地图区域时,这些注释会更新 我想使用pod“Cluster”,我添加了它,当我放大显示集群中的内容时,注释几乎在同一个位置出现了好几次(有时它会形成一个圆圈,见图) 我已经尝试过过滤我的列表,以便在集群中只获得一个注释实例,但它不起作用 每次用户更改区域时都会调用此方法: func createAnnotations(pois list:[PlaceModel]) { poiAnn
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
}
}
我尝试了几件没有成功的事。。
提前感谢。尝试使用
let annotation=ClusterAnnotation()
而不是annotation()
它不起作用,没有任何变化。您找到解决方案了吗?。因为我也有同样的问题。