How to calculate geography bounding box in iOS?

2020-07-13 03:13发布

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

2条回答
放我归山
2楼-- · 2020-07-13 04:07

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.

查看更多
Viruses.
3楼-- · 2020-07-13 04:11

I know I'm a bit late to the party, but I just built a class GTBoundingBox that might (or might not) help:

https://github.com/wpearse/ios-geotools

查看更多
登录 后发表回答