2011-10-17 8 views
0

iPhone MapKitを使用してマップアノテーション用のk-meansクラスタリングアルゴリズムを実装しようとしています。私はMKAnnotationを実装する2つのクラスを持っています:個々のポイントのPostLocationAnnotationとポイントのクラスターのLocationGroupAnnotation。次のようにLocationGroupAnnotationのヘッダである:私の地図表示制御装置においてNSDictionaryからNSMutableArrayにアイテムを追加する

@interface LocationGroupAnnotation : NSObject <MKAnnotation> 
{ 

    NSMutableArray *_locations; 
    CLLocationCoordinate2D _coordinate; 

} 

@property (nonatomic, retain) NSArray *locations; 
@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 

+ (LocationGroupAnnotation *)initWithLocations:(NSArray *)locationArray; 
+ (LocationGroupAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)coordinate; 
- (id)initWithLocations:(NSArray *)locationArray; 
- (id)initWithCoordinate:(CLLocationCoordinate2D)startCoordinate; 
- (void)addAnnotation:(PostLocationAnnotation *)annotation; 
- (NSUInteger)locationCount; 

@end 

、Iは(initWithCoordinate法を使用して)k個のLocationGroupAnnotationsを作成することによってLocationGroupAnnotationsにPostLocationAnnotationsのすべてを追加する方法を有するランダムに選択した場所で各その注釈からPostLocationAnnotationsを最も近いLocationGroupAnnotationsに追加する。このために、私はそうのような(「意味」は、この前に取り込まれていLocationGroupAnnotationsのNSMutableArrayのがある)NSMutableArrayのNSMutableDictionaryのオブジェクトを、使用します。

// find nearest cluster to each point 
NSMutableArray *distances = [[NSMutableArray alloc] initWithCapacity:[[ffMapView annotations] count]]; 

for (id <MKAnnotation> point in [ffMapView annotations]) 
{ 
    if ([point isKindOfClass:[PostLocationAnnotation class]]) 
    { 
     NSMutableDictionary *distanceDict = [[NSMutableDictionary alloc] initWithCapacity:2]; 

     [distanceDict setObject:point forKey:@"location"]; 

     double minDistance = 0; 
     LocationGroupAnnotation *closestMean = nil; 

     for (id <MKAnnotation> meanPoint in means) 
     { 
      if ([meanPoint isKindOfClass:[LocationGroupAnnotation class]]) 
      { 
       if (closestMean) 
       { 
        if ([ClusteringTools getDistanceBetweenPoint:[point coordinate] and:[meanPoint coordinate]] < minDistance) 
        { 
         closestMean = meanPoint; 
         minDistance = [ClusteringTools getDistanceBetweenPoint:[point coordinate] and:[meanPoint coordinate]]; 
        } 
       } else { 
        closestMean = meanPoint; 
        minDistance = [ClusteringTools getDistanceBetweenPoint:[point coordinate] and:[meanPoint coordinate]]; 
       } 
      } 
     } 

     [distanceDict setObject:closestMean forKey:@"closestMean"]; 

     [distances addObject:distanceDict]; 

     [distanceDict release]; 
    } 
} 

// add annotations to clusters 
for (NSDictionary *entry in distances) 
{ 
    [(LocationGroupAnnotation *)[entry objectForKey:@"closestMean"] addAnnotation:[entry objectForKey:@"location"]]; 
} 

私が午前問題があるのときI出力それぞれのlocationCountこの後のLocationGroupAnnotationでは、それぞれ1つにつき0が得られます。

- (void)addAnnotation:(PostLocationAnnotation *)annotation 
{ 
    [_locations addObject:annotation]; 
    NSLog(@"Added annotation at (%f,%f) to cluster %@", 
     [annotation coordinate].latitude, 
     [annotation coordinate].longitude, 
     [self description]); 
} 

...そして、それは、メモリアドレスから判断すべきであるところすべてが追加されているように見えます:私は(LocationGroupAnnotation中)のように、注釈が追加されるたびにログ出力を持っています。どうしたの?

答えて

2

_locationsはどこからでも初期化していますか?あなたが気をつけてnilにオブジェクトを追加するとエラーが起きないようです。

あなたは、コードを示していないが、あなたはまた、NSMutableArrayあるNSArrayで財産locationsとインスタンス変数_locationsは、あなたが物事をめちゃくちゃにすることができ、バックグラウンドでこれに巧妙な何かをやっているのか?これらの2つのプロパティをどのようにリンクしていますか?

+0

ビンゴ。 _locationsを初期化していません! – benwad

関連する問題