iOS-百度地图(增加点聚合功能)

基础demo(可直接运行):
http://pan.baidu.com/s/1kVB7EK3
博文连接:
http://www.cnblogs.com/hxwj/p/5146090.html
http://www.cnblogs.com/hxwj/p/4761080.html

百度地图点聚合和自定义标注

扩展-点聚合功能

在地图改变的时候传入坐标模型数组,使用百度地图的点聚合算法

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < 5; i ++) {
        FateModel *model = [FateModel new];
        model.lon = 116.404;
        model.lat = 39.915+i*0.05;
        [array addObject:model];
    }
     [self addPointJuheWithCoorArray:array];
}
//添加模型数组
- (void)addPointJuheWithCoorArray:(NSArray *)array {
    _clusterCaches = [[NSMutableArray alloc] init];
    for (NSInteger i = 3; i < 22; i++) {
        [_clusterCaches addObject:[NSMutableArray array]];
    }
    //点聚合管理类
    _clusterManager = [[BMKClusterManager alloc] init];
    [array enumerateObjectsUsingBlock:^(FateModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
        clusterItem.coor = CLLocationCoordinate2DMake(obj.lat, obj.lon);
        clusterItem.model = obj;
        [_clusterManager addClusterItem:clusterItem];
    }];
    [self updateClusters];
}

//更新聚合状态
- (void)updateClusters {
    _clusterZoom = (NSInteger)self.mapView.zoomLevel;
    @synchronized(_clusterCaches) {
        __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:(_clusterZoom - 3)];
        if (clusters.count > 0) {
            [self.mapView removeAnnotations:self.mapView.annotations];
            [self.mapView addAnnotations:clusters];
        } else {
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                ///获取聚合后的标注
                __block NSArray *array = [_clusterManager getClusters:_clusterZoom];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    //聚合后的数组
                    for (BMKCluster *item in array) {
                        FateMapAnnotation *annotation = [[FateMapAnnotation alloc] init];
                        annotation.coordinate = item.coordinate;
                        annotation.size = item.size;
                        annotation.cluster = item;
                        annotation.title = [NSString stringWithFormat:@"我是%ld个", item.size];
                        [clusters addObject:annotation];
                    }
                    [self.mapView removeAnnotations:self.mapView.annotations];
                    [self.mapView addAnnotations:clusters];
                });
            });
        }
    }
}

demo连接:
https://pan.baidu.com/s/1qXLuMCk

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

推荐阅读更多精彩内容

  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,200评论 12 197
  • Auto import(自动导入)对于 Windows,请依次转到“文件 (File)”>“设置 (Setting...
    acc8226阅读 308评论 0 0
  • 今天是公元二零一七年农历九月十七日,习惯计算农历的日子记得还是我那骚年在故乡的那些日子。今天虽一直呆在空间不...
    聚锦缘阅读 394评论 0 1
  • 首先我们创建一个类:(以下是类的代码) 这样我们的调用类就完成了,接下来是如何调用这个类 这样我们就可以推送了,我...
    浪子与渣男阅读 5,169评论 17 15
  • 1.想想今天做的事: 六点起床发口头祝福语,回复新年祝福,持续两三小时。 看双截棍教学视频练习双截棍。 去超多朋友...
    文建伟CZYH阅读 296评论 1 0