0

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];
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
luky
  • 2,263
  • 3
  • 22
  • 40
  • Why you don't use this code `[_map setCenterCoordinate:loc animated:YES]`? – Reinier Melian Aug 02 '16 at 16:07
  • I will try it, but something tells me this doesnt solve the problem. But will try it, thx. – luky Aug 03 '16 at 08:32
  • did you try? @luky – Reinier Melian Aug 03 '16 at 15:38
  • I am looking on it now, but it seems to me it is basicly similar to setRegion. I am not big expert of this. If I should use your solution I guess I would have to know how to transform my CLLocationCoordinate - like add some "meters" to latitude, to define the center which will be under covered part tho show my "real center point" above. But I have no Idea how much should I add to latitude, may be depending on the zoom. I will now look on the solution of OIDoor. Thanks – luky Aug 05 '16 at 11:59
  • Now I am thinking, may be the region span hints how much meters I should add. But I am now examining the second solution, may be I will try this later. Thx – luky Aug 05 '16 at 12:14

2 Answers2

2

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];
OlDor
  • 1,460
  • 12
  • 18
  • Hello. This worked as expected [_map setVisibleMapRect:[_map visibleMapRect] edgePadding:UIEdgeInsetsMake(0, 0, heightOfMapCover, 0) animated:YES]; Only problem is, that different values of bottom inset padding seems to force different map zoom. Like 100 is one zoom, 120 other zoom etc. Quite strange behaving of this. Otherwise it really shifts the map by the specified number what is what I needed. Thanks – luky Aug 05 '16 at 12:42
  • @luky, that issue sounds quite strange to me. I think zoom is controlled by `MKMapRect`, not the `UIEdgeInsets`. You can have a look at my original post here: http://stackoverflow.com/a/35614774/5165668. At the moment I am not sure whether this is your bug, or apple's bug. – OlDor Aug 06 '16 at 16:11
2

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.

Chris Brasino
  • 296
  • 2
  • 14