I’d like to recenter a MapKit map in an IPad application by code. It is nothing more, that panning the map to the given center at the original zoom level. No zoom in, no zoom out, just pan.
Here is what I do:
- (void)panToLatitude:(double)lat andLongitude: (double) lon sender: (id) sender
{
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = lat;
zoomLocation.longitude = lon;
MKCoordinateRegion viewRegion = MKCoordinateRegionMake(zoomLocation, [self.map region].span);
MKCoordinateRegion adjustedRegion = [self.map regionThatFits:viewRegion];
[self.map setRegion:adjustedRegion animated:YES];
}
It works, but sometimes (20-30% of the time) it changes the zoom level, it zooms out. I don’t want this, just recenter the map.
Any ideas?
Thanks
The map view is behaving correctly as in it is accurately displaying a region of the map using the Mercator projection. The distance between two meridians of longitude is larger as you get closer to the equator. Depending on where you’re panning from, the map view may have to adjust the zoom level to accommodate the region you have supplied.
For solutions, I would check out this category on MKMapView written by Troy Brant. It allows you to change the position of the map view whilst keeping a consistent zoom scale and does all the nerdy math stuff for you.
http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/