【问题标题】:Objective-c search locations with radiusObjective-c 使用半径搜索位置
【发布时间】:2011-06-01 01:29:48
【问题描述】:

objective-c 中是否有一个库可以让我指定半径和位置,以及位置列表并告诉我哪些位置在该半径内? 谢谢。

【问题讨论】:

  • 通过使用 sqrt((x-x0)^2+(y-y0)^2) 计算到位置 (x0,y0) 的距离,自己可能很容易做到

标签: objective-c cocoa-touch ios nsarray


【解决方案1】:

如果你有 CLLocations,那么这样的事情会起作用:

// Given NSArray *locations as an array of CLLocation* that you wish to filter

// and given a radius...
CLLocationDistance radius = kSomeRadius;

// and given a target you want to test against...
CLLocation* target = [[CLLocation alloc] initWithLatitude:someLat longitude:someLon];


NSArray *locationsWithinRadius = [locations objectsAtIndexes:
                                 [locations indexesOfObjectsPassingTest:
                                  ^BOOL(id obj, NSUInteger idx, BOOL *stop) {

                                      return [(CLLocation*)obj distanceFromLocation:target] < radius;

                                  }]];

[target release];

当然还有其他方法可以做到这一点。这只是一种方式。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 2013-05-06
    • 2016-09-06
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多