HACClusterMapViewController 类是用 Objective-C 编写的,便于在地图上有许多标记点时使用。这需要我们为任务构建一个超快速的数据结构。我们需要用 C 语言构建它以达到高性能。这个 2.2 版本是从原始作者的 2.0 版本派生而来的,增加了对代理和注释显示的更多灵活性,以及纠正了一些问题。
##需求和依赖项
##安装
####CocoaPods
pod 'HACClusterMapViewController'
####手动安装
HACQuadTree.h
、HACQuadTree.m
、HACManagerQuadTree.h
、HACManagerQuadTree.m
、HAClusterAnnotation.h
、HAClusterAnnotation.m
、HAClusterAnnotationView.h
、HAClusterAnnotationView.m
和 HACMKMapView.h
、HACMKMapView.m
复制到您的项目中##用法
在您的 .h 文件中导入 HACMKMapView
#import "HACMKMapView.h"
IBOutlet
为了使用 Interface Builder,请将您的 MKMapView 类设置为 HACMKMapView
,并将您的地图设置为 IBOutlet
,例如将名称设置给 IBOutlet 为 mapView
。
@interface MapViewController () <HACMKMapViewDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.mapDelegate = self;
}
使用很简单,您必须在循环中创建以下结构。
NSArray *data = @[
@{kLatitude:@48.47352, kLongitude:@3.87426, kTitle : @"Title 1", kSubtitle : @"", kIndex : @0},
@{kLatitude:@52.59758, kLongitude:@-1.93061, kTitle : @"Title 2", kSubtitle : @"Subtitle 2", kIndex : @1},
@{kLatitude:@48.41370, kLongitude:@3.43531, kTitle : @"Title 3", kSubtitle : @"Subtitle 3", kIndex : @2},
@{kLatitude:@48.31921, kLongitude:@18.10184, kTitle : @"Title 4", kSubtitle : @"Subtitle 4", kIndex : @3},
@{kLatitude:@47.84302, kLongitude:@22.81101, kTitle : @"Title 5", kSubtitle : @"Subtitle 5", kIndex : @4},
@{kLatitude:@60.88622, kLongitude:@26.83792, kTitle : @"Title 6", kSubtitle : @"" , kIndex : @5}
];
最后一步是将驱动程序的父级调用并传递数组作为参数以启动该过程。
[self.mapView.coordinateQuadTree buildTreeWithArray:data];
使用代理方法,我们可以设置注释的自定义图像或知道要引用的对象的索引。
-(void)viewForAnnotationView:(HAClusterAnnotationView *)annotationView annotation:(HAClusterAnnotation *)annotation{
if (annotation.index % 2 == 0) {
annotationView.image = [UIImage imageNamed:@"pin_museum"];
}else{
annotationView.image = [UIImage imageNamed:@"pin_coffee"];
}
}
-(void)didSelectAnnotationView:(HAClusterAnnotation *)annotation{
NSLog(@"You ara select annotation index %ld", (long)annotation.index);
}
self.mapView.backgroundAnnotation = [UIColor redColor];
self.mapView.borderAnnotation = [UIColor whiteColor];
self.mapView.textAnnotation = [UIColor whiteColor];
self.mapView.compassFrame = CGRectMake(10, 10, 25, 25);
self.mapView.legalFrame = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds)-50, CGRectGetHeight([UIScreen mainScreen].bounds)-50, 50, 50);
享受 :D
git checkout -b my-new-feature
)git commit -am '添加某些功能'
)git push origin my-new-feature
)