我的地图上有很多大头针,所以我认为将这些注释聚集在一起是个好主意。我不太确定如何在 iPhone 上实现这一点,我可以使用谷歌地图和一些 javascript 示例来解决问题。但是 iPhone 使用它的 mkmapview,我不知道如何在其中聚集注释。
您知道并且好的任何想法或框架?谢谢。
我的地图上有很多大头针,所以我认为将这些注释聚集在一起是个好主意。我不太确定如何在 iPhone 上实现这一点,我可以使用谷歌地图和一些 javascript 示例来解决问题。但是 iPhone 使用它的 mkmapview,我不知道如何在其中聚集注释。
您知道并且好的任何想法或框架?谢谢。
您不一定需要使用 3rd 方框架,因为从 iOS 4.2 开始,MKMapView 有一个方法调用- (NSSet *)annotationsInMapRect:(MKMapRect)mapRect
,您可以使用它来进行集群。
查看 WWDC11 会议视频“使用 MapKit 在地理上可视化信息”。大约一半的时候解释了如何去做。但我会为你总结一下这个概念:
-annotationsInMapRect
方法从不可见地图中获取注释数据幸运的是,您不再需要 3rd 方框架。iOS 11 具有原生集群支持。
你需要实现mapView:clusterAnnotationForMemberAnnotations:
方法。
在 Apple 示例中获取更多详细信息:https ://developer.apple.com/sample-code/wwdc/2017/MapKit-Sample.zip
由于这是一个非常常见的问题并且我需要一个解决方案,因此我编写了一个支持集群的 MKMapView 的自定义子类。然后我把它开源了!你可以在这里得到它:https ://github.com/yinkou/OCMapView 。
它管理注释的集群,您可以自己处理它们的视图。您无需执行任何操作,只需将该OCMapView
文件夹复制到您的项目中,在您的 nib 中创建一个MKMapView
并将其类设置为OCMapView
. (或者像常规一样在代码中创建和委托它MKMapView
)
通过使用 Apple 演示代码,很容易在我们的代码中实现集群概念。 参考链接
简单地说,我们可以使用以下代码进行聚类
实施聚类的步骤
Step1:重要的是聚类我们使用两个mapview(allAnnotationsMapView,),一个是参考(allAnnotationsMapView)。
@property (nonatomic, strong) MKMapView *allAnnotationsMapView;
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
在viewDidLoad
_allAnnotationsMapView = [[MKMapView alloc] initWithFrame:CGRectZero];
Step2 :将所有注解添加到_allAnnotationsMapView,在下面的_photos 是注解数组。
[_allAnnotationsMapView addAnnotations:_photos];
[self updateVisibleAnnotations];
Step3:添加以下聚类方法,在这个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;
}
}
通过以上步骤我们可以在mapview上实现聚类,不需要使用任何第三方代码或框架。请在此处查看Apple 示例代码。如果您对此有任何疑问,请告诉我。
你看过 ADClusterMapView 吗?https://github.com/applidium/ADClusterMapView
它正是这样做的。
我只是想对引脚进行聚类,只是显示它的编号。以下 https://www.cocoacontrols.com/controls/qtree-objc符合我的期望。
我最近分叉了另一个答案中提到的 ADClusterMapView 并解决了许多(如果不是全部)与该项目相关的问题。它是一种 kd-tree 算法,可以为聚类设置动画。
试试这个框架(XMapView.framework);它现在支持 iOS 8。
这个框架不需要你改变你当前的项目结构,它可以直接用于你的MKMapView。有一个 zip 文件。它为您提供了一次集群 200 个引脚的示例。在我用 iPod 测试后,我发现它非常流畅。
http://www.xuliu.info/xMapView.html
该库支持:
这里有一个非常酷且维护良好的 Objective-C 和 Swift 库:https ://github.com/bigfish24/ABFRealmMapView
由于它与Realm的集成,它的聚类效果非常好,并且还可以处理大量的点。