Search code examples
iosobjective-cmkmapview

Center MKMapView when part of map is covered


I have MKMapView which is from bottom covered by another view. Let's say the height of map is 250, but from bottom is 100 of it covered by other view.

Now, if I center the map using setRegion, it centers the map like if the whole map is visible, but I need to center it to region which is really visible, which is the rest 150 of height.

You can say, then lower the height of map to 150 so it won't be covered, but I need to have it covered by design, because the covering view does not have fully width to borders (there is gap from sides) so the map is visible around the covering view.

So, how to center the map for the height of the real visible region?

Now I am using this:

CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat, long);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (loc, 200, 200);
[_map setRegion:region animated:YES];

Solution

  • Try using:

    [theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
                         animated:YES];
    

    Or, if you would like to adjust the screen offsets a bit further, you can use:

    [theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
                      edgePadding:UIEdgeInsetsMake(50, 50, 50, 50)
                         animated:YES];