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

百度地圖點聚合和自定義標注
擴展-點聚合功能
在地圖改變的時候傳入坐標模型數(shù)組,使用百度地圖的點聚合算法
- (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];
}
//添加模型數(shù)組
- (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];
}
//更新聚合狀態(tài)
- (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(), ^{
//聚合后的數(shù)組
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