IPhone dev - shows two locations on the map
Now I have the coordinate of two places, say locationA with latitude 40 and longitude -80, locationB with latitude 30 and longitude -70.
I want to create a mapView so that I can see both locations with the corresponding view distance.
I got the new coordinate by finding the midpoint (in this example {35, -75}), but the question is that
How can I get a suitable viewing distance? Specifically, how can I calculate CLLocationDistance (if I use MKCoordinateRegionMakeWithDistance) or MKCoordinateSpan (if I use MKCoordinateSpanMake).
Thanks in advance.
a source to share
Here's what I figured out:
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:middlePoint.latitude longitude:middlePoint.longitude];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointB.latitude longitude:pointB.longitude];
CLLocationDistance d = [pointALocation distanceFromLocation:pointBLocation];
MKCoordinateRegion r = MKCoordinateRegionMakeWithDistance(middlePoint, 2*d, 2*d);
[mapView setRegion:r animated:YES];
The CLLocationDistance d value contains the distance (in meters) between the center and the second point you want to see. Then you use the midpoint and two distances in meters to adjust the region you want to see on the screen. By using 2 * d, I make sure there is enough room on the screen to display the second point.
Hope it helps.
- ank
a source to share