美文网首页iOS技术研究iOS开发高级地图集成
iOS-百度地图(增加点聚合功能)

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

作者: 幻想无极 | 来源:发表于2016-07-07 10:28 被阅读2908次

基础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

相关文章

网友评论

  • 62f99ca1c87b:发现1个严重bug。添加2000个点不会崩溃,当添加3000个点,内存一直持续增加直到崩溃。找了几个demo都是这样。应该是百度地图这个没处理好。
    幻想无极:@bridge_bd13 额,没试过那么多
  • SYSYSY:初始 有20个点,,,,然后我重新获取这个点聚合的数据后,地图还是显示20个字,等缩放才没有~
  • SYSYSY:问下楼主,,如何给点增加点击事件,跳转详情界面?
    幻想无极:@SYSYSY 你给你的视图加个回掉点击然后代理啊
    幻想无极:@SYSYSY 你➕就行了啊。
  • imbaWales:__block NSMutableArray *clusters = [_clusterCaches objectAtIndex:(_clusterZoom - 3)];
    想问下上面这段代码怎么理解 _clusterCaches是干嘛的
  • 翀鹰精灵:想问下,百度地图的这个聚合功能,聚合的比例自己控制吗?
    幻想无极:范围是自己控制的
  • _球比篮筐大:百度地图 点聚合 多个点相同坐标 怎么展开iOS
    幻想无极:坐标不能完全相同。
  • shLuckySeven:问下楼主,百度地图api的点聚合算法在哪看?我一直没找到
    世界的一缕曙光:请问楼主,如何对点聚合功能进行封装,供业务方去调用呢。
    shLuckySeven:@幻想_无极 找到了,谢谢
    幻想无极:代码里面加着的

本文标题:iOS-百度地图(增加点聚合功能)

本文链接:https://www.haomeiwen.com/subject/wzqljttx.html