How to calculate geography bounding box in iOS?

情到浓时终转凉″ 提交于 2019-12-21 12:59:46

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/12465711/how-to-calculate-geography-bounding-box-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!