我有很多标记需要放在我的地图上,所以我觉得将这些注释聚类会是一个不错的主意。我不太确定如何在 iPhone 上实现这一点,我能够使用 Google 地图和一些 JavaScript 示例解决了这个问题。但 iPhone 使用它的 mkmapview,我不知道如何在其中聚类注释。
你知道有什么好的想法或框架吗?谢谢。
我有很多标记需要放在我的地图上,所以我觉得将这些注释聚类会是一个不错的主意。我不太确定如何在 iPhone 上实现这一点,我能够使用 Google 地图和一些 JavaScript 示例解决了这个问题。但 iPhone 使用它的 mkmapview,我不知道如何在其中聚类注释。
你知道有什么好的想法或框架吗?谢谢。
自iOS 4.2以来,MKMapView有一个名为- (NSSet *)annotationsInMapRect:(MKMapRect)mapRect
的方法,您可以使用它进行聚类,因此您不一定需要使用第三方框架。
请查看WWDC11会话视频'使用MapKit在地理上可视化信息'。在视频的中间部分,它解释了如何做到这一点。但我将为您总结概念:
-annotationsInMapRect
方法从不可见地图获取注释数据很幸运,你不再需要第三方框架。iOS 11 原生支持聚合功能。
需要实现 mapView:clusterAnnotationForMemberAnnotations:
方法。
在苹果示例中获取更多详细信息:https://developer.apple.com/sample-code/wwdc/2017/MapKit-Sample.zip
由于这是一个非常普遍的问题,并且我需要解决方案,所以我编写了一个自定义的MKMapView子类来支持聚类。然后我将其开源!你可以在这里获取它:https://github.com/yinkou/OCMapView。
它管理标注的聚类,你可以自己处理它们的视图。
你只需要将 OCMapView
文件夹复制到你的项目中,在nib文件中创建一个MKMapView
并将其类设置为OCMapView
即可。(或者像使用普通的MKMapView
那样在代码中创建和委托它)
通过使用苹果的演示代码,我们可以轻松地在代码中实现聚类概念。参考链接
简单来说,我们可以使用以下代码进行聚类:
实现聚类的步骤:
步骤1: 聚类需要使用两个地图视图(allAnnotationsMapView),其中一个是用于参考的(allAnnotationsMapView)。
@property (nonatomic, strong) MKMapView *allAnnotationsMapView;
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
_allAnnotationsMapView = [[MKMapView alloc] initWithFrame:CGRectZero];
步骤2 : 将所有注释添加到_allAnnotationsMapView中,下面的_photos是注释数组。
[_allAnnotationsMapView addAnnotations:_photos];
[self updateVisibleAnnotations];
步骤3 : 添加以下用于聚类的方法,其中PhotoAnnotation是自定义注释。 MapViewDelegate方法
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated {
[self updateVisibleAnnotations];
}
- (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *annotationView in views) {
if (![annotationView.annotation isKindOfClass:[PhotoAnnotation class]]) {
continue;
}
PhotoAnnotation *annotation = (PhotoAnnotation *)annotationView.annotation;
if (annotation.clusterAnnotation != nil) {
// animate the annotation from it's old container's coordinate, to its actual coordinate
CLLocationCoordinate2D actualCoordinate = annotation.coordinate;
CLLocationCoordinate2D containerCoordinate = annotation.clusterAnnotation.coordinate;
// since it's displayed on the map, it is no longer contained by another annotation,
// (We couldn't reset this in -updateVisibleAnnotations because we needed the reference to it here
// to get the containerCoordinate)
annotation.clusterAnnotation = nil;
annotation.coordinate = containerCoordinate;
[UIView animateWithDuration:0.3 animations:^{
annotation.coordinate = actualCoordinate;
}];
}
}
}
- (id<MKAnnotation>)annotationInGrid:(MKMapRect)gridMapRect usingAnnotations:(NSSet *)annotations {
// first, see if one of the annotations we were already showing is in this mapRect
NSSet *visibleAnnotationsInBucket = [self.mapView annotationsInMapRect:gridMapRect];
NSSet *annotationsForGridSet = [annotations objectsPassingTest:^BOOL(id obj, BOOL *stop) {
BOOL returnValue = ([visibleAnnotationsInBucket containsObject:obj]);
if (returnValue)
{
*stop = YES;
}
return returnValue;
}];
if (annotationsForGridSet.count != 0) {
return [annotationsForGridSet anyObject];
}
// otherwise, sort the annotations based on their distance from the center of the grid square,
// then choose the one closest to the center to show
MKMapPoint centerMapPoint = MKMapPointMake(MKMapRectGetMidX(gridMapRect), MKMapRectGetMidY(gridMapRect));
NSArray *sortedAnnotations = [[annotations allObjects] sortedArrayUsingComparator:^(id obj1, id obj2) {
MKMapPoint mapPoint1 = MKMapPointForCoordinate(((id<MKAnnotation>)obj1).coordinate);
MKMapPoint mapPoint2 = MKMapPointForCoordinate(((id<MKAnnotation>)obj2).coordinate);
CLLocationDistance distance1 = MKMetersBetweenMapPoints(mapPoint1, centerMapPoint);
CLLocationDistance distance2 = MKMetersBetweenMapPoints(mapPoint2, centerMapPoint);
if (distance1 < distance2) {
return NSOrderedAscending;
} else if (distance1 > distance2) {
return NSOrderedDescending;
}
return NSOrderedSame;
}];
PhotoAnnotation *photoAnn = sortedAnnotations[0];
NSLog(@"lat long %f %f", photoAnn.coordinate.latitude, photoAnn.coordinate.longitude);
return sortedAnnotations[0];
}
- (void)updateVisibleAnnotations {
// This value to controls the number of off screen annotations are displayed.
// A bigger number means more annotations, less chance of seeing annotation views pop in but decreased performance.
// A smaller number means fewer annotations, more chance of seeing annotation views pop in but better performance.
static float marginFactor = 2.0;
// Adjust this roughly based on the dimensions of your annotations views.
// Bigger numbers more aggressively coalesce annotations (fewer annotations displayed but better performance).
// Numbers too small result in overlapping annotations views and too many annotations on screen.
static float bucketSize = 60.0;
// find all the annotations in the visible area + a wide margin to avoid popping annotation views in and out while panning the map.
MKMapRect visibleMapRect = [self.mapView visibleMapRect];
MKMapRect adjustedVisibleMapRect = MKMapRectInset(visibleMapRect, -marginFactor * visibleMapRect.size.width, -marginFactor * visibleMapRect.size.height);
// determine how wide each bucket will be, as a MKMapRect square
CLLocationCoordinate2D leftCoordinate = [self.mapView convertPoint:CGPointZero toCoordinateFromView:self.view];
CLLocationCoordinate2D rightCoordinate = [self.mapView convertPoint:CGPointMake(bucketSize, 0) toCoordinateFromView:self.view];
double gridSize = MKMapPointForCoordinate(rightCoordinate).x - MKMapPointForCoordinate(leftCoordinate).x;
MKMapRect gridMapRect = MKMapRectMake(0, 0, gridSize, gridSize);
// condense annotations, with a padding of two squares, around the visibleMapRect
double startX = floor(MKMapRectGetMinX(adjustedVisibleMapRect) / gridSize) * gridSize;
double startY = floor(MKMapRectGetMinY(adjustedVisibleMapRect) / gridSize) * gridSize;
double endX = floor(MKMapRectGetMaxX(adjustedVisibleMapRect) / gridSize) * gridSize;
double endY = floor(MKMapRectGetMaxY(adjustedVisibleMapRect) / gridSize) * gridSize;
// for each square in our grid, pick one annotation to show
gridMapRect.origin.y = startY;
while (MKMapRectGetMinY(gridMapRect) <= endY) {
gridMapRect.origin.x = startX;
while (MKMapRectGetMinX(gridMapRect) <= endX) {
NSSet *allAnnotationsInBucket = [self.allAnnotationsMapView annotationsInMapRect:gridMapRect];
NSSet *visibleAnnotationsInBucket = [self.mapView annotationsInMapRect:gridMapRect];
// we only care about PhotoAnnotations
NSMutableSet *filteredAnnotationsInBucket = [[allAnnotationsInBucket objectsPassingTest:^BOOL(id obj, BOOL *stop) {
return ([obj isKindOfClass:[PhotoAnnotation class]]);
}] mutableCopy];
if (filteredAnnotationsInBucket.count > 0) {
PhotoAnnotation *annotationForGrid = (PhotoAnnotation *)[self annotationInGrid:gridMapRect usingAnnotations:filteredAnnotationsInBucket];
[filteredAnnotationsInBucket removeObject:annotationForGrid];
// give the annotationForGrid a reference to all the annotations it will represent
annotationForGrid.containedAnnotations = [filteredAnnotationsInBucket allObjects];
[self.mapView addAnnotation:annotationForGrid];
for (PhotoAnnotation *annotation in filteredAnnotationsInBucket) {
// give all the other annotations a reference to the one which is representing them
annotation.clusterAnnotation = annotationForGrid;
annotation.containedAnnotations = nil;
// remove annotations which we've decided to cluster
if ([visibleAnnotationsInBucket containsObject:annotation]) {
CLLocationCoordinate2D actualCoordinate = annotation.coordinate;
[UIView animateWithDuration:0.3 animations:^{
annotation.coordinate = annotation.clusterAnnotation.coordinate;
} completion:^(BOOL finished) {
annotation.coordinate = actualCoordinate;
[self.mapView removeAnnotation:annotation];
}];
}
}
}
gridMapRect.origin.x += gridSize;
}
gridMapRect.origin.y += gridSize;
}
}
我只想对地图上的标记进行聚类,并显示它们的数量。以下这个https://www.cocoacontrols.com/controls/qtree-objc就符合我的要求。
你有没有看过ADClusterMapView?https://github.com/applidium/ADClusterMapView
它恰好可以做到这一点。
尝试使用这个框架(XMapView.framework);它现在支持iOS 8。
这个框架不需要你改变当前的项目结构,可以直接用于你的MKMapView。有一个压缩文件。它提供了一个一次聚合200个标记的示例。在iPod上测试后,我发现它非常流畅。
http://www.xuliu.info/xMapView.html
该库支持以下功能:
这里有一个非常酷炫且维护良好的库,适用于Objective-C和Swift:https://github.com/bigfish24/ABFRealmMapView
它能够很好地进行聚类,并且由于与Realm的集成,还可以处理大量的点。