Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 436
Brian Avatar asked Jan 19 '26 01:01

Brian


1 Answers

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

like image 137
Andres Kievsky Avatar answered Jan 21 '26 23:01

Andres Kievsky