如何搜索特定半径的位置?

时间:2013-05-13 00:37:07

标签: ios objective-c geolocation cllocationmanager clgeocoder

非常惊讶这很难搜索。我的搜索只显示我使用CLGeocoder以用户友好的格式显示信息。

我正在构建基于位置的应用,我有保存位置(到服务器)的功能,并在表格视图中列出它们。但是当我无法过滤和安排与我所在地区相关的位置时,这没什么用。

我应该在设备或php中设置位置半径吗?

我目前对解决方案的想法是将一个JSON坐标字符串发送到一个php脚本,该脚本通过MySQL数据库查找位置字段小于X远离用户位置的每个条目(通过粗略的' WHERE X< 34.23049823 AND X> 34.44789874和Y< ...)类型的东西。我不认为这是实现附近位置搜索功能的最简单或最快捷的方法。

1 个答案:

答案 0 :(得分:1)

当然,您可以在iOS中执行此操作。

// 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];

来源 - Objective-c search locations with radius

相关问题