I have a MKMapView and a never changing CLLocationCoordinate2D. What I am trying to do is center the map so that this coordinate will be placed at the bottom center of the map. I am able to center the map on this coordinate with the simple:
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapCenter, 10000, 10000);
[self.mapView setRegion:viewRegion animated:YES];
But how can I make it so that the map centers on the point that would make this mapCenter coordinate be placed at the bottom of the map? I would like it to work regardless of the initial zoom level of the map if possible.
I wrote you a quick method that should do the trick...
After getting a MKCoordinateRegion
with the location coordinate centered, you can create a new CLLocationCoordinate2D
center point by adding a fraction of that region's latitudinal span (in this case, a fourth). Create a new coordinate region using the new center point and the old region's span, set it as the region
of the MKMapView
, and you're good to go.
P.s. - If you want the location centered all the way at the bottom, create the new CLLocationCoordinate2D
center point by adding half of the region's latitudinal span (instead of a fourth).
-(void)setLocation:(CLLocationCoordinate2D)location inBottomCenterOfMapView:(MKMapView*)mapView
{
//Get the region (with the location centered) and the center point of that region
MKCoordinateRegion oldRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(location, 800, 800)];
CLLocationCoordinate2D centerPointOfOldRegion = oldRegion.center;
//Create a new center point (I added a quarter of oldRegion's latitudinal span)
CLLocationCoordinate2D centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/4.0, centerPointOfOldRegion.longitude);
//Create a new region with the new center point (same span as oldRegion)
MKCoordinateRegion newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span);
//Set the mapView's region
[worldView setRegion:newRegion animated:YES];
}
Great jop Matthew! Thanks!
Here the same solution in SWIFT 3:
func setLocation(location: CLLocationCoordinate2D, inBottomCenterOfMapView: MKMapView) {
let oldRegion = mapView.regionThatFits(MKCoordinateRegionMakeWithDistance(location, 800, 800))
let centerPointOfOldRegion = oldRegion.center
let centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/4.0, centerPointOfOldRegion.longitude)
let newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span)
worldView.setRegion(newRegion, animated: true)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With