在 iOS 上计算给定中心纬度/经度和距离的边界框?

zum*_*zum 3 latitude-longitude core-location ios

在我的 iOS 应用程序中,我有一个纬度和经度,可以告诉我我的位置。我希望能够计算出满足距我所在位置一定距离 d 的边界框。我怎样才能做到这一点?

尝试1: 所以我尝试了@Neeku给出的解决方案。我可以看到它应该如何返回正确的信息,但不幸的是它已关闭。所以我认为我不能使用它。

我写的代码是这样的,我通过了1000米:

MKCoordinateRegion startRegion = MKCoordinateRegionMakeWithDistance(center, meters, meters);

CLLocationCoordinate2D northWestCorner, southEastCorner;

northWestCorner.latitude = startRegion.center.latitude + .5 * startRegion.span.latitudeDelta;
northWestCorner.longitude = startRegion.center.longitude - .5 * startRegion.span.longitudeDelta;
southEastCorner.latitude = startRegion.center.latitude - .5 * startRegion.span.latitudeDelta;
southEastCorner.longitude = startRegion.center.longitude - .5 * startRegion.span.longitudeDelta;

NSLog(@"CENTER <%@,%@>", @(center.latitude),@(center.longitude));
NSLog(@"NW <%@,%@>, SE <%@,%@>",@(northWestCorner.latitude),@(northWestCorner.longitude),@(southEastCorner.latitude),@(southEastCorner.longitude));
Run Code Online (Sandbox Code Playgroud)

那么结果是: CENTER <38.0826682,46.3028721> NW <38.08717278501047,46.29717303828632>, SE <38.07816361498953,46.29717303828632>

然后我把它放在谷歌地图中并得到这个:(参见屏幕截图)

在此输入图像描述

所以根据我的理解,1000米应该是从盒子的中心到两侧。地图上测量的拐角应该有1000多米,实际上只有800多米。这就是我正在努力解决的问题。

我以前尝试过这种方法,但距离并不准确。所以,这个解决方案对我来说不起作用。如果您有更多建议或者可能想指出这里做错了什么,请告诉我。

谢谢

Rin*_*nov 5

假设您所需的距离是 111 米。然后您使用以下代码:

    // 111 kilometers / 1000 = 111 meters.
    // 1 degree of latitude = ~111 kilometers.
    // 1 / 1000 means an offset of coordinate by 111 meters.

    float offset = 1.0 / 1000.0;
    float latMax = location.latitude + offset;
    float latMin = location.latitude - offset;

    // With longitude, things are a bit more complex.
    // 1 degree of longitude = 111km only at equator (gradually shrinks to zero at the poles)
    // So need to take into account latitude too, using cos(lat).

    float lngOffset = offset * cos(location.latitude * M_PI / 180.0);
    float lngMax = location.longitude + lngOffset;
    float lngMin = location.longitude - lngOffset;
Run Code Online (Sandbox Code Playgroud)

latMax, latMin, lngMax,lngMin将为您提供边界框坐标。

(如果您需要 111 米以外的距离,您可以很容易地更改此代码。只需offset相应地更新变量即可)。