百度地图 聚合功能的实现
来源:互联网 发布:乐淘吧淘宝天猫商城 编辑:程序博客网 时间:2024/05/23 19:06
BMKClusterManager 的这样一个点聚合管理类.原理很简单,就是当地图缩放到某个级别的时候让地图显示另外一个类型的大头针.
百度地图聚合功能的实现:
{
BMKClusterManager *_clusterManager;//点聚合管理类
NSInteger _clusterZoom;//聚合级别
CGFloat _clusterFloatZoom;//百度地图聚合的浮点数级别
NSMutableArray *_clusterCaches;//点聚合缓存标注
NSInteger _previousZoom;// 记录前一次的缩放等级
}
#pragma mark - 更新聚合状态
//更新聚合状态
- (void)updateClusters {
_clusterZoom = (NSInteger)_mapView.zoomLevel;
_clusterFloatZoom=_mapView.zoomLevel;
// NSLog(@"当前更新地图的时候地图的缩放级别为: %f",_mapView.zoomLevel);
// NSLog(@"当前更新地图时地图的缩放级别取整结果: %ld",_clusterZoom);
//NSLog(@"--现在当前的地图缩放级别为 %d",_clusterZoom);
if (_clusterZoom<11) { // 目前是当地图缩放级别小于 11 级的时候才开始聚合
if (_dataSource.count <150) {
return;
}
[selfgetStationModelToJuHeAnnotation];
@synchronized(_clusterCaches) {
__blockNSMutableArray *clusters = [_clusterCachesobjectAtIndex:(_clusterZoom -3)];
if (clusters.count >0) {
[_mapViewremoveAnnotations:_mapView.annotations];
[_mapViewaddAnnotations:clusters];
[_mapViewaddAnnotation:_selfLocalModel];
} else {
dispatch_async(dispatch_get_global_queue(0,0), ^{
///获取聚合后的标注
__blockNSArray *array = [_clusterManagergetClusters:_clusterFloatZoom];
dispatch_async(dispatch_get_main_queue(), ^{
for (BMKCluster *itemin array) {
//获得每一个聚合之后标注的内容(个数,经纬度)
ClusterAnnotation *annotation = [[ClusterAnnotationalloc] init];
annotation.coordinate = item.coordinate;
annotation.size = item.size;
annotation.title = [NSStringstringWithFormat:@"我是%ld个", item.size];
[clusters addObject:annotation];
int yellow=0;
int blue=0;
int gray=0;
for (JuHeModel* modelin item.clusterItems) {
if ([model.categaryintValue] ==3) {
yellow++;
}elseif ([model.categaryintValue] == 2){
blue++;
}elseif([model.categaryintValue] ==1){
gray++;
}
}
annotation.yellowCount=[NSStringstringWithFormat:@"%d",yellow];
annotation.blewCount=[NSStringstringWithFormat:@"%d",blue];
annotation.grayCount=[NSStringstringWithFormat:@"%d",gray];
}
[_mapViewremoveAnnotations:_mapView.annotations];
[_mapViewaddAnnotations:clusters];
[_mapViewaddAnnotation:_selfLocalModel];
});
});
}
}
}else{
if (_previousZoom!= (NSInteger)_mapView.zoomLevel ) {
[_mapViewremoveAnnotations:_mapView.annotations];
[_mapViewaddAnnotations:_dataSource];
[_mapViewaddAnnotation:_selfLocalModel];
}
}
// 记录前一次的缩放比例
_previousZoom=(NSInteger)_mapView.zoomLevel;
}
-(void)getStationModelToJuHeAnnotation
{
[selfaddJuHecoordinate:_dataSource];
}
-(void)addJuHecoordinate:(NSArray*)models
{
//点聚合管理类
_clusterManager = [[BMKClusterManageralloc] init];
for (LocalModel* modelin models) {
JuHeModel* clusterItem=[[JuHeModelalloc] init];
clusterItem.coor=model.coordinate;
clusterItem.categary=model.category;
[_clusterManageraddClusterItem:clusterItem];
}
}
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
NSLog(@"地图代理中区域发生变化时当前地图的所缩放级别:%f",mapView.zoomLevel);
if (_previousZoom != (NSInteger)mapView.zoomLevel) {
[selfupdateClusters];
}
}
// 根据anntation生成对应的View
/**
ClusterAnnotation 遵从BMKAnnotation 协议的大头针模型
ClusterAnnotationView 为自定义的聚合模型,目前展示的是外面一圈色带展示所聚合区域类不同类型的占比
*/
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
[selfhideHud];
//普通annotation
if ([annotationisKindOfClass:[ClusterAnnotationclass]]) { // 聚合类
NSString *AnnotationViewID =@"ClusterMark";
ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;
ClusterAnnotationView *annotationView = [[ClusterAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:AnnotationViewID];
annotationView.size = cluster.size;
//按返回比例计算
// annotationView.bluScal=[cluster.blewCount intValue] * 1.0 / (cluster.size * 1.0);
// annotationView.yellowScal=[cluster.yellowCount intValue] * 1.0 / (cluster.size * 1.0);
// annotationView.grayScal=[cluster.grayCount intValue] * 1.0 / (cluster.size * 1.0);
// 周围只选择蓝色一圈
annotationView.bluScal=1.0;
// NSLog(@"%d--%d %f %f %f",cluster.size,cluster.size-2, annotationView.bluScal,annotationView.yellowScal,annotationView.grayScal);
annotationView.draggable =YES;
//annotationView.canShowCallout=NO;
annotationView.annotation = cluster;
return annotationView;
}elseif ([annotation isKindOfClass:[LocalModelclass]]) { // 非聚合类
/** 根据数据模型 想展示怎样的大头针自己定义*/
return newAnnotationView;
}
returnnil;
}
项目中的聚合效果图:
- 百度地图 聚合功能的实现
- 使用百度地图的点聚合功能
- android项目中实现百度地图中点聚合功能
- 百度地图API 重新生成点聚合的功能
- Android百度地图点聚合功能
- Android:百度地图 点聚合功能
- 百度地图开发之点聚合功能
- Android:百度地图,点聚合功能
- Android_百度地图,点聚合功能,点击事件&&设置聚合数字的背景颜色
- 百度地图定位功能的实现
- 百度地图路线规划功能的实现
- 百度地图实现简单的定位功能
- 百度地图点聚合开发-地图找房功能
- 百度地图点聚合实现方法
- Android百度地图点聚合功能的子项目点击事件
- 百度地图聚合点的展示
- 关于百度地图点聚合的问题
- 百度地图简要功能实现
- postgresql数据类型varchar长度
- 【Android基础知识】Fragment设计哲学和加载方式
- HTTP断点续传与断点上传之 -- 文件流操作
- tcp socket客户端发送请求连接http服务
- Eclipse下更换主题-Sublime的主题
- 百度地图 聚合功能的实现
- Java设计模式之装饰模式
- 1029. Median (25)
- Android开发应用安装出现两个相同应用的解决办法
- apache开启页面压缩
- source insight研究——正则表达式篇(转)
- oracle中delect和truncate区别
- Windows 10安装.NET 3.5的方法(满足sqiserver2012之需求)
- Mysql体系结构详解——后台线程(待更新)