0
好像很多人已经涉及这个重叠MKAnnotation视图的问题,并且在缩放结束时以及在viewdidload处优雅地展开它们。所以我认为我只是硬着头皮开发出一个解决方案,但在此之前,我想询问算法的实施。关于这个话题,有没有提出可行的解决方案曾任:传播MapView注释的功能,使得没有任何重叠
ios sdk MKMapView overlapping pins? Laying out overlapping rectangles
还有更多...
所以我想到的功能是这样的:(注意:所有的CGRects是同样大小)
-(NSArray *)spreadOutTheseViews:(NSArray *)arrayofCGRects{
////////////////////////////////////
//find out who overlaps with who..
////////////////////////////////////
NSMutableArray *arrayOfArraysOfRects = [[NSMutableArray alloc] init];
// 1. make an array to let you know which Rects have already been added to one of the sets
int n = (int)[arrayofCGRects count];
NSMutableArray *addedAlready = [[NSMutableArray alloc] init];
for (id obj in arrayofCGRects) {
[addedAlready addObject:@0];
}
// 2. go through and add them if they overlap
for (int i=0; i<[arrayofCGRects count]; i++) {
if ([[addedAlready objectAtIndex:i] isEqualToValue:@(0)]) {
CGRect rect1 = [[arrayOfArraysOfRects objectAtIndex:i] CGRectValue];
[arrayOfArraysOfRects addObject:[[NSMutableArray alloc] initWithObjects:@(i), nil]];
[addedAlready insertObject:@(1) atIndex:i];
for (int j=0; j<[arrayofCGRects count]; j++){
if (i!=j && [[addedAlready objectAtIndex:j] isEqualToValue:@(0)]) {
CGRect rect2 = [[arrayOfArraysOfRects objectAtIndex:j] CGRectValue];
if (CGRectIntersectsRect(rect1,rect2)){
// check if they are overlapping, if so, add it to
[[arrayOfArraysOfRects lastObject] addObject:@(j)];
[addedAlready insertObject:@(1) atIndex:j];
}
}
}
}
}
NSMutableArray *arrayofCGPoints = [[NSMutableArray alloc] init];
//3. now that we have "clusters" of overlapping rects.. push them away from each other..
for(NSArray *indexArray in arrayOfArraysOfRects){
if ([indexArray count]==1){
// just simply add the CGPoint
CGRect rect = [[arrayofCGRects objectAtIndex:(NSInteger)[indexArray firstObject]] CGRectValue];
[arrayofCGPoints addObject:[NSValue valueWithCGPoint:rect.origin] ];
}else{
// heart of the algo..
// Idea 1: get the average of the CGPoints to find the center of all of them, then find a ring with a specific radius that we can put them all on. Note this is a packing problem that I really dont have a better solution for..
}
}
return arrayofCGPoints;
}
这里任何帮助将不胜感激..
澄清问题:您是在谈论点聚类还是关于注释视图由于美学原因彼此不重叠? – incanus 2014-11-10 18:42:30
是的,后者,在这里更好地演示:http://stackoverflow.com/questions/17710770/ios-sdk-mkmapview-overlapping-pins – fchaubard 2014-11-10 23:58:46