luky
luky

Reputation: 2370

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];

Upvotes: 0

Views: 951

Answers (2)

Chris Brasino
Chris Brasino

Reputation: 296

Not sure if you ever figured this out but here is a solution in swift. Should be easy to adapt for Obj-C

let pointYouWantCentered = CLLocationCoordinate2D(latitude: lat, longitude: lon)
pointYouWantCentered.latitude -= (mapView.region.span.latitudeDelta * 0.25)
mapView.setCenter(pointYouWantCentered, animated: true)

This will center the point in the top half of the screen. As long as you know the portion of the screen that your bottom view takes up you can adjust the center of the map to be on the point you want. For instance this is centering the point in the top half of the screen, assuming you have a view taking up the bottom half. If your bottom view took up 1/3 of the bottom and you wanted to center on the top 2/3 of the view you would do the following.

let pointYouWantCentered = CLLocationCoordinate2D(latitude: lat, longitude: lon)
pointYouWantCentered.latitude -= (mapView.region.span.latitudeDelta * (1.0/6.0))
mapView.setCenter(pointYouWantCentered, animated: true)

Hope this helps.

Upvotes: 2

OlDor
OlDor

Reputation: 1466

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];

Upvotes: 2

Related Questions