Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate geography bounding box in iOS?

I'd like to make a Geographic Bounding box Calculation in iOS. It can be aprox.

Input Parameters:

Current Location (Example: 41.145495, −73.994901)

Radius In Meters: (Example: 2000)

Required Output:

MinLong: (Example: 41.9995495)

MinLat: (Example: −74.004901)

MaxLong: (Example: 41.0005495)

MaxLat: (Example: −73.004901)

Requirement: No Network Call

Any Ideas? Mapkit / CoreLocation does not seem to offer this type of thing?

Any other Geographic SDK that i could use?

Thanks

like image 738
ActionFactory Avatar asked Sep 17 '12 19:09

ActionFactory


1 Answers

I think you can use standard MapKit functions: MKCoordinateRegionMakeWithDistance, this will return a MKCoordinateRegion, which is really just a center point (lat, lon) and the spans in the latitudal and longitudal direction in degrees. Add/subtract half of the span from the latitude and longitude respectively and you have the values you're looking for.

CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(41.145495, −73.994901);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(centerCoord, 2000, 2000);

double latMin = region.center.latitude - .5 * startRegion.span.latitudeDelta;
double latMax = region.center.latitude + .5 * startRegion.span.latitudeDelta;
double lonMin = region.center.longitude - .5 * startRegion.span.longitudeDelta;
double lonMax = region.center.longitude + .5 * startRegion.span.longitudeDelta;

By the way: this is only representative for the longitude for small spans, in the order of a couple of kilometers. To quote Apple:

latitudeDelta
The amount of north-to-south distance (measured in degrees) to use for the span. Unlike longitudinal distances, which vary based on the latitude, one degree of latitude is approximately 111 kilometers (69 miles) at all times.

longitudeDelta
The amount of east-to-west distance (measured in degrees) to use for the span. The number of kilometers spanned by a longitude range varies based on the current latitude. For example, one degree of longitude spans a distance of approximately 111 kilometers (69 miles) at the equator but shrinks to 0 kilometers at the poles.

like image 78
Thijs Kuipers Avatar answered Sep 23 '22 03:09

Thijs Kuipers