Brian
Brian

Reputation: 235

iPhone dev - showing two locations on the map

Now I have the coordinate of two locations, let say locationA with latitude 40 and longitude -80, locationB with latitude 30 and longitude -70,

I want to create a mapView that I can see both locations with appropriate viewing distance.

I got the new coordinate by finding the midpoint (in this example, {35, -75}), but the question is,

How can I get an appropriate viewing distance? In particular, how can I calculate CLLocationDistance (if I'm using MKCoordinateRegionMakeWithDistance) or MKCoordinateSpan (if I'm using MKCoordinateSpanMake).

Thanks in advance.

Upvotes: 2

Views: 1983

Answers (1)

Andres Kievsky
Andres Kievsky

Reputation: 3491

This is what I've 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 contains the distance (in meters) between the center and the second point you want to see. You then use the middle point and two distances in meters to setup the region that you want visible on the screen. By using 2*d, I make sure the screen will have enough space to display the second point.

Hope it helps.

-- ank

Upvotes: 11

Related Questions