3

I'm trying to have a map view centre on one point so the user can smoothly zoom in and out but remain centred on that point.

I've a less than optimal solution by centring the map when regionDidChangeAnimated is called, plus a flag to stop the code looping infinitely...

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    if (!self.isRecentring) {
        self.isRecentring = YES;
        [mapView setCenterCoordinate:self.centreLocation animated:YES];
        self.isRecentring = NO;
    }
}

So, that works but only does its thing once the user has finished changing the zoom, meaning there's a quick scroll to reposition the map afterward.

I've tried the equivalent with regionWillChangeAnimated but that just kills the zoom dead, I'm presuming because my setCentreCoordinate sets a new region and ends the zoom gesture?

Any ideas how I can work around this and maintain the centre point mid-zoom?

bcl
  • 478
  • 1
  • 5
  • 16
  • Does setting `scrollEnabled = NO` work? – Jesse Rusak May 13 '14 at 17:12
  • No, sorry- no joy with scrollEnabled = NO. The region still seems to centre around the point the user initiates the zoom gesture. – bcl May 13 '14 at 17:18
  • Related: http://stackoverflow.com/questions/6006600/maintain-centre-coordinate-while-pinching-mkmapview and http://stackoverflow.com/questions/11830516/prevent-scrolling-in-a-mkmapview-also-when-zooming –  May 13 '14 at 18:04
  • Thanks for the suggestions but those aren't doing it either. The first solution is written to work with the user's current location (and when I modified it to work with a different location it stopped the zoom working entirely). The second solution has no immediate feedback (only resizes and centres upon ending the pinch gesture) so in my opinion is almost as inelegant as my approach above. – bcl May 24 '14 at 16:42
  • 1
    Did you ever figure out a solution for this? – Jordan Brown Sep 01 '14 at 22:18
  • Sorry- only just noticed your question :( No, I never did find an elegant solution to this. In the end I just went for a standard (i.e. user pan-able) map and added a button to return to the centre point if they got 'lost'. – bcl Sep 22 '14 at 00:17

1 Answers1

0

I know this is old, but I came up with a solution that doesn't appear to have any rendering issues.

  1. Set scrollEnabled, zoomEnabled, and rotateEnabled to false on the mapView
  2. Add a UIPinchGestureRecognizer to the map view
  3. In the pinch gesture recognizer handler, save the region at the start of the gesture. You can use recognizer.state == .began to detect this.
  4. In the pinch gesture recognizer handler, when recognizer.state == .changed, multiply the start region's span.longitudeDelta and span.latitudeDelta by recognizer.scale and perform a mapView.setRegion with no animation.
corym
  • 382
  • 1
  • 4
  • 14