【问题标题】:What is the best strategy to sort entities by distance from current location on iOS在iOS上按距当前位置的距离对实体进行排序的最佳策略是什么
【发布时间】:2011-10-09 05:57:34
【问题描述】:

我需要显示按距离该产品公司最近的地址排序的产品列表。

我有一个像这样的绳索数据结构:

产品 公司 > 地址

address 实体有一个用于纬度和经度的 NSNumber 字段。 我还有一个共享单例,它可以获取并存储当前的CLLocation

获取和排序的最佳策略是什么。

方法一

向 Address 实体添加一个只读的 distance 属性,该属性从 lat/lng 属性创建一个 CLLocation 并返回计算出的距离与共享单例中存储的 currentLocation 之间的距离。这可能有利于让我使用 NSPredicate 进行排序和过滤。我想知道它是否会产生性能成本。

方法二

只需将所有产品提取到一个数组中,然后循环遍历该数组即可手动对其进行排序。

【问题讨论】:

    标签: objective-c ios core-data


    【解决方案1】:

    当我这样做时,我在单例中使用了这个:

    - (NSNumber*) distanceToLocation:(CLLocationCoordinate2D) location{
        if (!self.isValidLocation) return nil;
    
    
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:location.latitude longitude:location.longitude];    
        CLLocationDistance distance=[self.locationManager.location distanceFromLocation:loc];
        return [NSNumber numberWithDouble:distance];
    

    }

    然后地址实体的只读属性调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多