【问题标题】:Objective C: Filtering based on location目标 C:基于位置的过滤
【发布时间】:2025-12-21 08:40:06
【问题描述】:

我的问题是基于CLLocationManager 并过滤一定半径内的位置。

我有什么:

  1. 用户的位置(取自CLLocationManager),
  2. 数组中的say存储列表,每个存储的坐标以纬度和经度为单位。

我的要求是我需要过滤掉用户给定位置半径 100 公里内的商店。

另外,如果 100 公里半径内有超过 25 家商店,则逻辑需要转移到过滤掉离我所在位置最近的 25 家商店。

解决此问题的最佳方法是什么?

【问题讨论】:

标签: ios objective-c location cllocationmanager latitude-longitude


【解决方案1】:

最好的选择是使用许多数据库的内置功能为您进行过滤和排序。

基本实现非常明显:只需对商店进行排序并获得位于定义区域内的前 25 个。

鉴于您可能拥有数千家商店,这是我可能会写它的优化版本:

NSArray *stores = ...;
CLLocation *myLocation = ...;

NSMutableArray *nearestStores = [NSMutableArray array];
CLRegion *nearest100kmRegion = [CLRegion initCircularRegionWithCenter:[myLocation coordinate]
                                                               radius:100000
                                                           identifier:@"someIdentifier"];

// We will need to enumerate all stores to ensure that there are no more
// than 25 objects within the defined region.
//
// Since there may be thousands of objects and many of them can be
// out of the defined region, we should perform concurrent enumeration
// for performance reasons.
dispatch_semaphore s = dispatch_semaphore_create(1);
[stores enumerateObjectsWithOptions:NSEnumerationConcurrent
                         usingBlock:^(id store, NSUInteger idx, BOOL *stop)
{
     if ([nearest100kmRegion containsCoordinate:[[store location] coordinate]])
     {
         dispatch_semaphore_wait(s, DISPATCH_FOREVER);
         [nearestStores addObject:store];
         dispatch_semaphore_signal(s);
     }
}];
dispatch_release(s);

if ([nearestStores count] > 25)
{
    [nearestStores sortWithOptions:NSSortConcurrent
                   usingComparator:^(id store1, id store2)
    {
        return [myLocation distanceFromLocation:[store1 location]] - [myLocation distanceFromLocation:[store2 location]];
    }];
}

return [nearestStores subarrayWithRange:NSMakeRange(0, MAX([nearestStores count], 25))];

【讨论】:

    【解决方案2】:

    请参阅Calculate distance between two latitude-longitude points? (Haversine formula) 以确定 2 个纬度/经度对之间的距离,然后执行以下操作:

    按与用户的距离对商店数组进行排序,然后循环遍历数组以填充结果集。当结果集计数为 25 或距离大于 100km 时停止。

    如果商店的数组真的比你应该首先用 100 公里内的商店填充一个数组,然后如果 count

    【讨论】: