I m trying to make an implementation of a k-means clustering algorithm for map annotations using the iPhone MapKit. I ve got 2 classes which implement MKAnnotation: PostLocationAnnotation for individual points and LocationGroupAnnotation for clusters of points. The header for LocationGroupAnnotation is as follows:
@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
在我的地图观察控制员中,我有办法将所有邮政电传给各地点组别,每个地点都采用从说明中随机挑选的地点,并将邮政电传给最接近的地点组。 为此,我使用一架NSMutable Array的有害物质,如此(是指在座前有人居住的Suntable Array):
// 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"]];
}
我面临的问题是,我在此之后将每个地点组别的科恩混为一谈。 每次添加通知时,我都有一个记录产出(在地点组别):
- (void)addAnnotation:(PostLocationAnnotation *)annotation
{
[_locations addObject:annotation];
NSLog(@"Added annotation at (%f,%f) to cluster %@",
[annotation coordinate].latitude,
[annotation coordinate].longitude,
[self description]);
}
......而且,它想像一切照样,以记忆地址判断。 因此,情况如何?