I have a square MKMapView in my app, and I wish to set a center point and the exact height/width of the view in meters.
Creating an MKCoordinateRegion and setting the map to it (as in this code...
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, 1000.0, 1000.0);
[self.mapView setRegion:region animated:YES];
..) doesn't work properly because using regions here just means that at least that region is displayed, typically more than the region is.
I'm planning on using setVisibleMapRect:animated: method instead, as I believe this will zoom to the actual MKMapRect passed.
So, is there a simple way to convert between an MKcoordinateRegion and an MKMapRect? Perhaps getting the top-left and bottom-right coordinates of the region, and using them to the make the MKMapRect?
I couldn't see anything handy in the Map Kit Functions Reference.
(Using iOS 5, Xcode 4.2)
Use the built in function
MKCoordinateRegionForMapRect
To add another implementation to the pile:
NB: There are many ways to convert between
MKMapRect
andMKCoordinateRegion
. This one certainly is not the exact inverse ofMKCoordinateRegionMakeWithDistance()
, but approximates it fairly well. So, be careful converting back and forth, because information can be lost.you can use method to convert
MKCoordinateRegion
toCGRect
and use
- (MKMapRect)mapRectForRect:(CGRect)rect
or use
MKMapPointForCoordinate
method to first convert coordinates toMKPoint
and use that to formMKMapRect
to eventually usesetVisibleMapRect:animated: