Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom annotation view is being removed from its superview after setting map view center coordinate
    primarykey
    data
    text
    <p>I'm working on a location-aware application that is using MKMapView with custom callout bubbles implemented as MKAnnotationView subclass. Yesterday I've encountered a subtle bug when custom annotation view is not being shown. After investigating this issue a bit, I've come up with following results:</p> <ul> <li>this issue manifests itself only if I'm trying to show custom annotation view immediately after zooming with pinch gesture. So for example if you pinch and then pan the map view everything works fine. Programmatically changing map zoom doesn't cause this issue to appear either</li> <li><p>setting breakpoint in didMoveToSuperview in my custom annotation view class reveals the following backtrace:</p> <pre><code>#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:] () </code></pre></li> </ul> <p>Here, CalloutMapAnnotationView is my custom annotation view class. <code>adjustMapRegionIfNeeded</code> method adjusts map view's center coordinate if annotation is too close to map borders which in turn causes removal of CalloutMapAnnotationView instance from its superview. Why this is happening and what a workaround may be?</p> <p>Further investigation showed even more strange behavior. I added a bunch of debug NSLogs in <code>adjustMapRegionIfNeeded</code> to print each annotation visibility and came up up with following result:</p> <p><strong>Normal case (custom annotation is displayed):</strong></p> <pre><code>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 </code></pre> <p><strong>Custom annotation is not displayed:</strong></p> <pre><code>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 </code></pre> <p>Despite parent annotation and custom callout annotation having the same location, one of them is visible while the other is not. I'm testing annotation visibility with the following code:</p> <pre><code>[[self.mapView annotationsInMapRect:self.mapView.visibleMapRect] containsObject:self.annotation] </code></pre> <p>More than that, the following assertion <strong>fails</strong>:</p> <pre><code>MKMapRect visibleMapRect = self.mapView.visibleMapRect; MKMapPoint annotationPoint = MKMapPointForCoordinate(self.annotation.coordinate); NSAssert(MKMapRectContainsPoint(visibleMapRect, annotationPoint) == [[self.mapView annotationsInMapRect:visibleMapRect] containsObject:self.annotation], @"?!"); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload