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

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

iOS-百度地图(增加点聚合功能)_第1张图片
百度地图点聚合和自定义标注

扩展-点聚合功能

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

- (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

你可能感兴趣的:(iOS-百度地图(增加点聚合功能))