MKMapKit学习总结(四)聚合

背景

在高德sdk中提供了,聚合功能,那么使用回苹果自带的地图mkmapkit怎么来实现聚合的功能呢?mkmapkit本身就提供看聚合的功能,但是要注意的是,只是在iOS11以后才可以。iOS11以前就找其他方法吧,或者放弃吧😀

介绍

聚合用到的最重要的类就是
MKMarkerAnnotationView
当然这里主要讲一下用法,具体的介绍可以取找一下,苹果官方的sdk说明。

当然可以理解
MKMarkerAnnotationView 就是 MKAnnotationView的一个子类主要为了实现聚合功能的类。

直接开搞

  • 第一步:
    我们这里新建一个类继承MKMarkerAnnotationView
    @interface JGMarkerAnnotationView : MKMarkerAnnotationView

然后在.m的实现里面。
::重要的来了::

@interface JGMarkerAnnotationView()
@property (nonatomic, strong) NSString *imageName;
@end
@implementation JGMarkerAnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
     if (self) {
         _imageName = [NSString stringWithFormat:@“飞机图标_%@“ ,annotation.subtitle];
         self.displayPriority = MKFeatureDisplayPriorityDefaultLow;
         self.glyphTintColor = [UIColor blueColor];
         self.clusteringIdentifier = @“uav”;
         self.collisionMode = MKAnnotationViewCollisionModeCircle;
         [self resetMarkGlyph:NO];  
     }
    return self;   
}
- (void)resetMarkGlyph:(BOOL)isCluster
{
    if (isCluster) {
        self.markerTintColor = [UIColor whiteColor];
        self.glyphText = nil;
        self.image = nil;
    }
    else {
        self.markerTintColor = [UIColor clearColor];
        self.glyphText = @"";
        //设置标注的图片
        self.image = [UIImage imageNamed:_imageName];
        if (self.image == nil) {
            self.image = [UIImage imageNamed:@"飞机图标_0_sync"];
        }
    }
}

上面就是对这个view上的图片和聚合以后的文字,样式进行设置。
然后最关键的一句话是:
self.clusteringIdentifier = @“uav”;
在这里,对每一个可聚合的view设置了一个id。可以理解为,如果这个对地图进行zoom放大或者缩小的时候,如果形成聚合。会根据这个id,如果id相同,就会聚合到一起,展示数量的label。

  • 第二步
    我们要去mkmapkit的代理中去关联你新建的这个JGMarkerAnnotationView
    我们需要在比较靠前的地方先去注册这个JGMarkerAnnotationView类,这里的实现方式,我觉得可以用对tabview的实现方式,来理解这里。
    tabview的实现方式,先注册一个cell的类,然后去代理中服用。这里也是一样的。
    ::重要::
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 初始化地图
***********
//
   [_mapView registerClass:[JGMarkerAnnotationView class] forAnnotationViewWithReuseIdentifier:MKMapViewDefaultAnnotationViewReuseIdentifier];
 }

然后就是在代理中,关联一下

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
   JGMarkerAnnotationView *annotationView  = (JGMarkerAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@“mark”];  
    if (!annotationView) {
        annotationView = [[JGMarkerAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MKMapViewDefaultAnnotationViewReuseIdentifier];
    }
    if ([annotation isKindOfClass:[JGAnnotation class]]) {
        [annotationView resetMarkGlyph:NO];
    }
    else if ([annotation isKindOfClass:[MKClusterAnnotation class]]) {
        [annotationView resetMarkGlyph:YES];
    }
    return annotationView;
}

好,这里就算完成了,中间的一个if和else if为了显示聚合和非聚合情况下,显示的一下区别。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,383评论 0 1
  • 今天是她的祭日。 到今天,她离开我就——落笔至此,不禁悚然一惊,我竟不能即刻说出究竟几年——有时觉得已经很久了,曾...
    潇湘夜雨11阅读 246评论 0 4
  • 今年是个特殊的假期,因为疫情全国人民都呆在家里。现在慢慢遏制住了疫情。大家可以走出家门去外面呼吸新鲜空气。 ...
    小女人的足迹阅读 234评论 0 2