Question

Using MapKit (iOS 7), when you zoom to the userLocation it will place that location in the center of the "visible portion" of the mapView.

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
   {
      MKCoordinateRegion mapRegion;
      mapRegion.center = mapView.userLocation.coordinate;
      mapRegion.span.latitudeDelta = 0.2;
      mapRegion.span.longitudeDelta = 0.2;
     [mapView setRegion:mapRegion animated: YES];
   }

This means if you have a navigation bar that it will consider the center to be:

   CGFloat y = (self.mapView.bounds.size.height - self.navigationController.navigationBar.bounds.size.height) / 2.0;
   CGFloat x = self.mapView.bounds.size.width / 2.0;
   CGPoint userLocationPoint = CGPointMake(x, y);

The same is true for the statusBar, toolbars, etc... I can see how this would be advantageous but I'd prefer to not have it in my app. Is there a way to tell the mapView to disregard these insets?

Was it helpful?

Solution

I've solved this by calculating the offset I desire and setting a new region.

// Update iVar when region changes
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    CGFloat screenHeight = self.view.frame.size.height;
    CGFloat otherControlsHeight = self.toolbarView.frame.size.height + self.collectionView.frame.size.height + self.navigationController.navigationBar.frame.size.height;
    CGFloat pointOffset = (screenHeight - otherControlsHeight);
    CGFloat percentage = pointOffset / screenHeight;
    latitudeOffset = mapView.region.span.latitudeDelta / 2.0 * percentage;

}


// Then whenever you set your centerCoordinate or region
MKCoordinateRegion region = self.mapView.region;
region.center = CLLocationCoordinate2DMake(region.center.latitude - latitudeOffset, region.center.longitude);
[self.mapView setRegion:region animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top