我的 mapView 有一个自定义注释。我最初使用一些数据在其上设置坐标、标题(例如“第一个标题”)、子标题(例如“第一个地址”)、用户 ID 和距离(例如 0 米)属性。我将它添加到 mapView 和数组中以备后用。一切正常,它显示在 mapView 上,我按下它,标注显示初始数据。
后来我得知该标注的位置已更改。我遍历数组并使用坐标、标题(例如“新标题”)、子标题(例如“新地址”)和距离(例如 100 米)属性的新数据更新标注。我还将标注从它的原始位置动画到它的新位置。动画效果很好,标注从 A 点移动到 B 点。
问题是当我点击注释时,旧数据会显示在标注上而不是新数据上。
我calloutAccessoryControlTapped
用来推一个新的vc。当我在那里放置断点时,自定义引脚具有所有新数据。错误似乎发生在标注上。
我该如何解决?
我不想清除 mapView 中的所有注释,所以这不是一个选项。我打电话mapView.removeAnnotation(customPin)
并mapView.addAnnotation(customPin)
解决了该图钉的问题,但是当图钉被移除并添加回地图时会闪烁,然后当它动画到它的新位置时它看起来不连贯。
自定义注释
class CustomPin: NSObject, MKAnnotation {
@objc dynamic var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var userId: String?
var distance: CLLocationDistance?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, userId: String, distance: CLLocationDistance?) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.userId = userId
self.distance = distance
super.init()
}
}
第一次使用初始数据设置注释
firstFunctionThatGetsTheInitialLocation(origLat, origLon) {
let firstCoordinate = CLLocationCoordinate2DMake(origLat, origLon)
let distanceInMeters: CLLocationDistance = self.center.distance(from: anotherUsersLocation)
let customPin = CustomPin(coordinate: firstCoordinate, title: "first title", subtitle: "first address", userId: "12345", distance: distance)
DispatchQueue.main.async { [weak self] in
self?.mapView.addAnnotation(customPin)
self?.arrOfPins.append(customPin)
}
}
第二次使用新数据设置注释
secondFunctionThatGetsTheNewLocation(newCoordinate: CLLocationCoordinate2D, newDistance: CLLocationDistance) {
for pin in customPins {
pin.title = "second title" // ** updates but the callout doesn't reflect it
pin.subTitle = "second address" // ** updates but the callout doesn't reflect it
pin.distance = newDistance // ** updates but the callout doesn't reflect it
// calling these gives me the new data but the annotation blinks and moves really fast to it's new location
// mapView.removeAnnotation(pin)
// mapView.addAnnotation(pin)
UIView.animate(withDuration: 1) {
pin.coordinate = newCoordinate // this updates and animates to the new location with no problem
}
}
}
MapView 视图用于注解
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) { return nil }
guard let annotation = annotation as? CustomPin else { return nil }
let reuseIdentifier = "CustomPin"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView?.image = UIImage(named: "chevronImage")
} else {
annotationView?.annotation = annotation
}
annotationView?.detailCalloutAccessoryView = nil
annotationView?.detailCalloutAccessoryView = createCallOutWithDataFrom(customPin: annotation)
return annotationView
}
为标注创建 UIView
func createCallOutWithDataFrom(customPin: CustomPin) -> UIView {
let titleText = customPin.title
let subTitleText = customPin.subTitle
let distanceText = subTitle.distance // gets converted to a string
// 1. create a UIView
// 2. create some labels and add the text from the title, subTitle, and distance and add them as subViews to the UIView
// 3. return the UIView
}