Iphone 设置地图视图中心坐标后,自定义注释视图将从其superview中删除
我正在开发一个位置感知应用程序,该应用程序使用MKMapView,并将自定义标注气泡实现为MKAnnotationView子类。昨天,当自定义注释视图未显示时,我遇到了一个微妙的错误。在对这个问题进行了一些调查之后,我得出了以下结果:Iphone 设置地图视图中心坐标后,自定义注释视图将从其superview中删除,iphone,ios,mapkit,mkannotation,mkannotationview,Iphone,Ios,Mapkit,Mkannotation,Mkannotationview,我正在开发一个位置感知应用程序,该应用程序使用MKMapView,并将自定义标注气泡实现为MKAnnotationView子类。昨天,当自定义注释视图未显示时,我遇到了一个微妙的错误。在对这个问题进行了一些调查之后,我得出了以下结果: 只有当我尝试在使用收缩手势缩放后立即显示自定义注释视图时,此问题才会出现。因此,例如,如果你捏然后平移地图视图,一切都很好。以编程方式更改地图缩放也不会导致出现此问题 在自定义注释视图类的didMoveToSuperview中设置断点会显示以下回溯: #0 -
- 只有当我尝试在使用收缩手势缩放后立即显示自定义注释视图时,此问题才会出现。因此,例如,如果你捏然后平移地图视图,一切都很好。以编程方式更改地图缩放也不会导致出现此问题
- 在自定义注释视图类的didMoveToSuperview中设置断点会显示以下回溯:
#0 -[CalloutMapAnnotationView didMoveToSuperview] #1 0x00186857 in -[UIView(Hierarchy) removeFromSuperview] () #2 0x00e14c70 in -[MKAnnotationContainerView _removeAnnotationView:updateCollections:] () #3 0x00e196cb in -[MKAnnotationContainerView _removeAnnotationViews:] () #4 0x00e19f51 in -[MKAnnotationContainerView _displayAnnotationsInMapRect:includePending:animated:removeOffscreenAnnotations:] () #5 0x00e1aaa7 in -[MKAnnotationContainerView _refreshDisplayedAnnotations] () #6 0x00dfc508 in -[MKMapView _didChangeRegionMidstream:centerPoint:] () #7 0x00e0165c in -[MKMapView _goToCenterCoordinate:zoomLevel:animationType:] () #8 0x00df34c3 in -[MKMapView goToCenterCoordinate:zoomLevel:animationType:] () #9 0x00e0086f in -[MKMapView setCenterCoordinate:animated:] () #10 0x00036fc3 in -[CalloutMapAnnotationView adjustMapRegionIfNeeded] #11 0x00037c63 in -[CalloutMapAnnotationView didMoveToSuperview] #12 0x0017f750 in -[UIView(Internal) _addSubview:positioned:relativeTo:] () #13 0x0017dc00 in -[UIView(Hierarchy) insertSubview:atIndex:] () #14 0x00e2049f in -[MKAnnotationContainerView _addViewForAnnotation:] () #15 0x00e199a5 in -[MKAnnotationContainerView _addViewsForAnnotations:animated:] () #16 0x00e19f0d in -[MKAnnotationContainerView _displayAnnotationsInMapRect:includePending:animated:removeOffscreenAnnotations:] () #17 0x00e1a9e2 in -[MKAnnotationContainerView showAddedAnnotationsAnimated:] ()
adjustMapRegionIfNeeded
如果注释离地图边框太近,则该方法会调整地图视图的中心坐标,从而导致从其superview中删除Callout MapAnnotationView实例。为什么会发生这种情况,解决办法是什么
进一步的调查显示了更奇怪的行为。我在AdjustMapRegionInneed
中添加了一组调试NSLogs来打印每个注释可见性,并得出以下结果:
正常情况(显示自定义注释):
Custom callout annotation location: (55.821350, 37.497490)
Parent annotation location: (55.821350, 37.497490)
Custom callout annotation visibility before adjustment: 1
Custom callout annotation visibility after adjustment: 1
Parent annotation visibility: 1
Custom callout annotation location: (55.821350, 37.497490)
Parent annotation location: (55.821350, 37.497490)
Custom callout annotation visibility before adjustment: 1
Custom callout annotation visibility after adjustment: 0
Parent annotation visibility: 1
不显示自定义批注:
Custom callout annotation location: (55.821350, 37.497490)
Parent annotation location: (55.821350, 37.497490)
Custom callout annotation visibility before adjustment: 1
Custom callout annotation visibility after adjustment: 1
Parent annotation visibility: 1
Custom callout annotation location: (55.821350, 37.497490)
Parent annotation location: (55.821350, 37.497490)
Custom callout annotation visibility before adjustment: 1
Custom callout annotation visibility after adjustment: 0
Parent annotation visibility: 1
尽管父注释和自定义详图索引注释具有相同的位置,但其中一个注释可见,而另一个注释不可见。我正在使用以下代码测试注释可见性:
[[self.mapView annotationsInMapRect:self.mapView.visibleMapRect] containsObject:self.annotation]
除此之外,以下断言失败:
MKMapRect visibleMapRect = self.mapView.visibleMapRect;
MKMapPoint annotationPoint = MKMapPointForCoordinate(self.annotation.coordinate);
NSAssert(MKMapRectContainsPoint(visibleMapRect, annotationPoint) == [[self.mapView annotationsInMapRect:visibleMapRect] containsObject:self.annotation], @"?!");
我遇到了同样的问题(并且很可能使用了与您相同的教程) 我已将一些NSlog放入
CalloutAnnotationView
的didMoveToSuperView
中,发现添加CalloutAnnotationView
后,其superview(MKAnnotationContainerView的一个实例)正在发布。这可能意味着在放大或缩小时,MKMapView
会在内部重新创建其MKAnnotationContainerView
我所做的是在添加它之后做一些检查:
[self.mapView addAnnotation: m_calloutAnnotation];
[self performSelector: @selector(checkCalloutAnnotationVisibility)
withObject: nil
afterDelay: 0.15];
...
- (void) checkCalloutAnnotationVisibility
{
if (!m_calloutAnnotationView.window)
{
[self.mapView removeAnnotation: m_calloutAnnotation];
[self.mapView addAnnotation: m_calloutAnnotation];
[self performSelector: @selector(checkCalloutAnnotationVisibility)
withObject: nil
afterDelay: 0.1];
}
}
...
- (void) mapView: (MKMapView *) mapView
didDeselectAnnotationView: (MKAnnotationView *) view
{
[NSObject cancelPreviousPerformRequestsWithTarget: self
selector:
@selector(checkCalloutAnnotationVisibility)
object: nil];
...
}
是的,它相当粗糙。如果您找到了更好的解决方案,请发帖子:) 我通过将区域调整代码移动到
mapView:didAddAnnotationViews:
中解决了这个问题,这似乎为我解决了问题。现在这是一种更优雅的解决方法。美好的现在要摆脱快速修复。。。