1

CLLocationManager私の質問は、特定の半径内の場所に基づいてフィルタリングしています。

私が持っているもの:

  1. ユーザーの場所 (から取得CLLocationManager)、
  2. 緯度と経度の座標を持つ配列内の店舗のリスト。

私の要件は、ユーザーが指定した場所から半径 100 km 以内にある店舗を除外する必要があるということです。

また、半径 100 km 内に 25 を超える店舗がある場合、ロジックは、私の場所に最も近い 25 の店舗を除外するように移行する必要があります。

これについて最善の方法は何ですか?

4

2 に答える 2

2

最良のオプションは、多くのデータベースの組み込み機能を使用して、フィルタリングと順序付けを行うことです。

基本的な実装は非常に明白です。店舗を並べ替えて、定義された領域内にある最初の 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))];
于 2013-03-15T06:33:15.833 に答える
0

2 つの緯度経度ポイント間の距離を計算するを参照してください。(Haversine 式) 2 つの緯度と経度のペア間の距離を決定するには、次のようにします。

店舗の配列をユーザーまでの距離で並べ替え、配列をループして結果セットを作成します。結果セット数が 25 の場合、または距離が 100km を超えた場合に停止します。

店舗の配列が非常に大きい場合は、最初に 100 km 以内の店舗を配列に入力する必要があります。count <= 25 の場合はリスト全体を返します。それ以外の場合は並べ替えて、並べ替えられた配列から最初の 25 を返します。

于 2013-03-15T06:11:53.077 に答える