2011-10-17 51 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

相关问题