I have this code :
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D userLocation = CLLocationCoordinate2DMake(48.9793946200, 2.4726272850);
CLLocationDistance dist1 = 636.9887048804;
CLLocationDistance dist2 = 900.8380655203;
CLLocationDistance dist = dist1;
[self.myMapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation, dist, dist) animated:YES];
// TEST
// ------------------------------------------------------------
MKCoordinateRegion region = self.myMapView.region;
CLLocationDegrees lat = region.center.latitude;
CLLocationDegrees lon = region.center.longitude - region.span.longitudeDelta/2;
CLLocation *west = [[[CLLocation alloc] initWithLatitude:lat longitude:lon] autorelease];
NSLog(@"User location: lat : %.10lf long : %.10lf", userLocation.latitude, userLocation.longitude);
NSLog(@"distance set: %.10lfm", dist);
NSLog(@"center: lat : %.8lf long : %.8lf", region.center.latitude, region.center.longitude);
CLLocation* centerRegion = [[[CLLocation alloc] initWithLatitude:region.center.latitude longitude:region.center.longitude] autorelease];
NSLog(@"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:west]);
lat = region.center.latitude - region.span.latitudeDelta/2 ;
lon = region.center.longitude;
CLLocation *north = [[[CLLocation alloc] initWithLatitude:lat longitude:lon] autorelease];
NSLog(@"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
// ------------------------------------------------------------
}
When setting dist = dist1, that gives :
User location: lat : 48.9793946200 long : 2.4726272850
distance set: 636.9887048804m
center: lat : 48.97937199 long : 2.47269630
distance to western boundary: 500.44m
distance to western boundary: 650.57m
When setting dist = dist2, that gives :
User location: lat : 48.9793946200 long : 2.4726272850
distance set: 900.8380655203m
center: lat : 48.97937199 long : 2.47269630
distance to western boundary: 500.44m
distance to western boundary: 650.57m
What's the problem here ? Why do I have the same display with 2 different distances ?
Final question : How can I be sure to display the wanted meters on the map, at minimum for horizontal and vertical visual (with or without animation of course) ?
If I understand it correctly, you want to tell the mapView "give me a map which is 636m across" or "give me a map which is 900m across". But the map gives you the same distance across for both of these.
When you set a map region, you generally don't get back exactly what you ask for, but instead a best fit. The map view looks at the region you requested, then creates a region which fits your region inside it. The problem is that the map view doesn't zoom exactly to your requested region, it finds the highest zoom level that allows all your region to be visible. It won't use an in-between zoom level. This is why when you use setRegion:
the map always looks crisp. You can manually set an in-between zoom level by pinching in or out, but not (as far as I know) programatically.
Note too that the map view might change the actual region
variable you pass to it. Here's the docs:
When setting a new region, the map may adjust the value in the region parameter so that it fits the visible area of the map precisely. This is normal and is done to ensure that the value in the region property always reflects the visible portion of the map. However, it does mean that if you get the value of that property right after calling this method, the returned value may not match the value you set. (You can use the regionThatFits: method to determine the region that will actually be set by the map.)
You can see the difference in regions by logging the region you give it, and the one the map view actually sets (although I didn't see the passed myRegion
change):
MKCoordinateRegion myRegion = MKCoordinateRegionMakeWithDistance(userLocation, dist, dist);
NSLog(@"Passed: %f %f", myRegion.span.latitudeDelta, myRegion.span.longitudeDelta);
[self.mapView setRegion:myRegion animated:YES];
NSLog(@"Passed 2: %f %f", myRegion.span.latitudeDelta, myRegion.span.longitudeDelta);
NSLog(@"Set: %f %f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
> Passed: 0.005728 0.008702
> Passed 2: 0.005728 0.008702
> Set: 0.012957 0.013733
If you bump your distance up to 1200m, you'll be able to see the next zoom level.
BTW, there's a small error in your code:
NSLog(@"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
should be
NSLog(@"distance to northern boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
One possible cause of this is that the region you are specifying has square aspect ratio while your MKMapView
probably has a rectangular one.
When you set the region MKMapView
will not use it exactly as it is, but will modify it so that:
Thus if your view has a width:height aspect ratio of 2:1, then the west/east boundaries would be 200m from the center while north/south boundaries 100m from the center.
Something to try after setting the region as above:
MKCoordinateRegion region = self.mapView.region;
CLLocationDegrees lat = region.center.latitude;
CLLocationDegrees lon = region.center.longitude - region.span.longitudeDelta/2;
CLLocation *west = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
NSLog(@"distance to western boundary: %.2lfm", [userLocation distanceFromLocation:west]);
lat = region.center.latitude + region.span.latitudeDelta/2
lon = region.center.longitude;
CLLocation *north = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
NSLog(@"distance to northern boundary: %.2lfm", [userLocation distanceFromLocation:north]);
One of those should be 100m. If not, I would be interested to see what they are.
P.S. The code above has not been tested in anyway.
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