I’m trying to have a custom MKAnnotationView (PostFlag) do two things when it is selected: 1) expand to show details about the annotation, and 2) become centered in the map. The trouble is that when the annotation is selected, the MKMapView sometimes dramatically zooms out. This causes my logic for handling significant zoom events to run, which collects nearby annotations into common annotation views, causing the annotation views to reload, which often replaces the annotation the user was trying to view with a closed annotation view.
My attempts to fix this started by trying to be sure that I don’t set the region incorrectly (with a span that would cause the zoom-out for example) when I try to center the annotation. Everything there seems ok – the region is correct when I set it. The delegate method mapView:didChangeRegion:animated: still gets called though with a zoomed out region. I’m not sure exactly what’s going on there.
Setting scrollEnabled and zoomEnabled to NO before changing the region to center the annotation doesn’t seem to have any effect.
It doesn’t matter if I animate the expansion of the annotation view or not – the zooming still occurs.
Does this sound familiar to anyone? Here’s some code:
- (void) mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
self.selectedFlag = (PostFlag*)view; //disables scrolling/zooming on the map
//move region to show expanded flag
float size = (m_selectedFlag.expandedHeight / 2.0) + 50; //getting a comfortable position in the display
float dLat = size * mapView.region.span.latitudeDelta / mapView.frame.size.height; //get span in degrees from degree to pixel ratio
//build a new coordinate for our center location
CLLocationCoordinate2D originalCoord = ((PostAnnotation*)m_selectedFlag.annotation).coordinate;
CLLocationCoordinate2D offsetCoord = CLLocationCoordinate2DMake(originalCoord.latitude + dLat, originalCoord.longitude);
//adjust the map view to center on that new coordinate
[m_mapView setRegion: MKCoordinateRegionMake(offsetCoord, m_mapRegionAtTimeOfLastUpdate.span) animated: YES];
}
- (void) setSelectedFlag:(PostFlag *) val
{
if(m_selectedFlag)
{
m_selectedFlag.expanded = NO;
[m_selectedFlag release];
}
if(val)
m_selectedFlag = [val retain];
else
m_selectedFlag = nil;
if(m_selectedFlag)
{
m_selectedFlag.expanded = YES;
[m_mapView bringSubviewToFront: m_selectedFlag];
m_mapView.scrollEnabled = NO;
m_mapView.zoomEnabled = NO;
}
else
{
m_mapView.scrollEnabled = YES;
m_mapView.zoomEnabled = YES;
}
}
When a PostFlag collapses, it tells this view controller and the vc re-enabled scrolling and zooming on the map. So, just to be clear, while the annotation view is expanded, the zoom and scroll on the map are turned off.
- (void) postFlagWasCollapsed: (PostFlag*) flag
{
[self.expandedFlags removeObject: flag];
if([self.expandedFlags count] == 0)
{
m_mapView.scrollEnabled = YES;
m_mapView.zoomEnabled = YES;
}
}
Also, just to clarify, I’m keeping an array of what annotation views are expanded. Since the user can tap a nearby annotation view and expand it while the previously expanded one closes, there may be a lapse where two annotation views are expanded simultaneously. Therefor, to be sure I don’t turn the scrolling/zooming back on until all annotations are closed, I use the array.
- (void) postFlagWasExpanded: (PostFlag*) flag
{
if(!self.expandedFlags)
self.expandedFlags = [NSMutableArray array];
if([self.expandedFlags indexOfObject: flag] == NSNotFound)
[self.expandedFlags addObject: flag];
}
So that’s probably more details than you wanted. But if anyone has any idea as to why the map view is zooming out when annotation views are selected, please chime in. Thanks in advance!
I don’t think scrollEnabled means what you think it means. It stops the user from scrolling, but it doesn’t stop your code from telling the map to scroll. Same goes for zooms.
“If you set the value of this property to NO, you may still change the map location programmatically by changing the value in the region property.”
Ref: http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/scrollEnabled
So forget meddling with scrollEnabled. Look again at your setRegion call. I would think your span is coming out wrong. Log the region and tell us what it sets when it is doing what you want and when it radically zooms out.