2011-10-17 88 views
0

我正在嘗試使用iPhone MapKit爲映射註釋實現k-means聚類算法。我有兩個實現MKAnnotation的類:PostPointLocationAnnotation用於各個點,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具有通過創建ķLocationGroupAnnotations(使用initWithCoordinate方法)隨機每一個位置來選擇添加所有的PostLocationAnnotations到LocationGroupAnnotations的方法從註釋開始,並將PostLocationAnnotations添加到最近的LocationGroupAnnotations。對於這一點,我使用的NSMutableDictionary的NSMutableArray中一個對象,像這樣(「裝置」是已經在此之前被填充LocationGroupAnnotations的一個NSMutableArray):

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

我遇到的問題是,當我輸出的每個的locationCount LocationGroupAnnotation在此之後,我爲每個人獲得0。我每次註釋添加,像這樣(在LocationGroupAnnotation)時間日誌輸出:

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

...它看起來像被添加一切都在它應該由存儲器地址判斷。發生什麼了?

回答

2

你是否在任何地方初始化了_locations?看起來你可能會愉快地將對象添加到nil,這不會導致任何錯誤。

您還沒有顯示的代碼,但你也有一個屬性locations這是一個NSArray和實例變量_locations這是一個NSMutableArray,你在這可能搞亂事情的背景做什麼巧妙搭配呢?你如何將這兩個屬性連接在一起?

+0

賓果。不初始化_locations! – benwad

相關問題