iOS-百度地图点聚合

一、坐标点转换

坐标分为:平面坐标、球面坐标(地理经纬度);在地图应用上使用的平面坐标,如果拿到的是地理经纬度坐标,那么是要进行转换的

-(void)Convert{
            
      //北纬N22°48′26.53″  东经E113°33′47.24″ ,拆分为:度 22  、分48、秒26.53 
      /*----先进行六十进制转换 WGS84坐标----*/
      double  s = 26.53/60; 
      double  f = ( 48 + s )/60;
      double  lat = 22 + f;

  //这时候 c 是 WGS84:为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系;
 //百度地图SDK在国内(包括港澳台)使用的是BD09坐标;在海外地区,统一使用WGS84坐标

    /*----BD09坐标转换----*/
    
    //下面是百度官方提供的转换,每应用提供转换是不一样的,虽然是同一个地方,但是转换后的坐标点是不一样的,这里转换后的坐标点只适用百度地图
   CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(lat,lon);//原始坐标

    //转换国测局坐标(google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标)至百度坐标
   NSDictionary* testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_COMMON);

    //转换WGS84坐标至百度坐标(加密后的坐标)
    testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_GPS);
    
    //解密加密后的坐标字典
    CLLocationCoordinate2D baiduCoor = BMKCoorDictionaryDecode(testdic);

    double y = baiduCoor.latitude;
    double x = baiduCoor.longitude;
    NSLog(@"百度地图BD09坐标 --- %f,%f",y,x);
}

二、添加坐标点、数据

-(void)onClickReverseGeocode
{
     array_description = [NSMutableArray array];
     for (int i = 0; i< 5; i++) {

          MapPointModel * model = [MapPointModel new];  //
          model.lat =  23.195090+(i*0.00002);
          model.lon = 113.260530;
          model.title = [NSString stringWithFormat:@"坐标:%d",I];
          [array_description addObject:model];
      }
      [self addPointJuheWithCoorArray:array_description];  //添加模型数组
}

三、添加模型数组

NSMutableArray *_clusterCaches;//点聚合缓存标注
BMKClusterManager *_clusterManager;//点聚合管理类

- (void)addPointJuheWithCoorArray:(NSArray *)array {

  _clusterCaches = [[NSMutableArray alloc] init];
  for (NSInteger i = 3; i < 22; i++) {
        [_clusterCaches addObject:[NSMutableArray array]];
  }
  //点聚合管理类
  _clusterManager = nil;
  _clusterManager = [[BMKClusterManager alloc] init];
  
  for (int i = 0; i <array.count; i++) {
    
     MapPointModel * model = array[i];
     BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
     clusterItem.coor = CLLocationCoordinate2DMake(model.lat, model.lon);
     clusterItem.model = model;
     [self->_clusterManager addClusterItem:clusterItem];
  }

     [self updateClusters]; //更新聚合状态

}

四、更新聚合状态

NSInteger _clusterZoom;//聚合级别
BMKMapView * _mapView;

- (void)updateClusters {
  _clusterZoom = (NSInteger)_mapView.zoomLevel;
  @synchronized(_clusterCaches) {
      __block NSMutableArray *clusters = [_clusterCaches objectAtIndex:(_clusterZoom - 3)];
      if (clusters.count > 0) {
          [_mapView removeAnnotations:_mapView.annotations];
          [_mapView addAnnotations:clusters];
      } else {
          dispatch_async(dispatch_get_global_queue(0, 0), ^{

              ///获取聚合后的标注
              __block NSArray *array = [self->_clusterManager getClusters:self->_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;
           
                      if (item.size == 1) { //坐标点没有重叠的时候
                          BMKClusterItem *clusterItem = item.clusterItems[0];
                          MapPointModel * model = clusterItem.model;
                          annotation.title = model.title;
                       }
                        annotation.title = [NSString stringWithFormat:@"我是%lu个", (unsigned long)item.size];
                        [clusters addObject:annotation];
                   }
                  [self->_mapView removeAnnotations:self->_mapView.annotations];
                  [self->_mapView addAnnotations:clusters];
              });
          });
       }
   }

 }

//地图改变的时候发送请求

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    // 
    [self updateClusters];
}
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
    [self updateClusters];
}


- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation{
    // 生成重用标示identifier    
    FateMapAnnotation *cluster = (FateMapAnnotation*)annotation;

    NSString *AnnotationViewID = @"xidanMark";

    // 检查是否有重用的缓存
    FateMapAnnotationView* annotationView = (FateMapAnnotationView*)[view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    // 缓存没有命中,自己构造一个,一般首次添加annotation代码会运行到此处
    if (annotationView == nil) {
        annotationView = [[FateMapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        annotationView.paopaoView = nil;
    }

    annotationView.size = cluster.size;
    annotationView.cluster = cluster.cluster;
    annotationView.annotation = cluster;
    annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));

    // 单击弹出泡泡,弹出泡泡前提annotation必须实现title属性
    annotationView.canShowCallout = YES;

  //cluster.size 当前聚合状态坐标系个数,当一个的时候给它做一些你想要做的事情😏
   if (cluster.size == 1 ) { //其中一个点只有一个坐标,添加点击弹出的泡泡View
    
        double popViewH = 165;
        
        UIView * popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 220, popViewH)];
        [popView.layer setMasksToBounds:YES];
        [popView.layer setCornerRadius:3.0];
        popView.alpha = 0.9;
        UIImageView * imgView = [[UIImageView alloc]initWithFrame:popView.bounds];
        imgView.image = [UIImage imageNamed:@"红色气泡"];
        [popView addSubview:imgView];
        
        BMKActionPaopaoView * pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
        pView.frame = CGRectMake(0, 0, 130, popViewH-15);
        
        ((FateMapAnnotationView *)annotationView).paopaoView = pView;
        popView = ((FateMapAnnotationView *)annotationView).paopaoView ;
        //
        UILabel * label_title = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 210, 35)];
        [self labelStyle: label_title with:imgView with:cluster.title];
        
   }

   return annotationView;

 }


-(void)labelStyle:(UILabel *)lable with:(UIImageView *)imgView with:(id)data{
    lable.text = [NSString stringWithFormat:@"%@",data];
    lable.textColor = [UIColor whiteColor];
    lable.font = [UIFont systemFontOfSize:10];
    lable.numberOfLines = 0;
    lable.adjustsFontSizeToFitWidth = YES;
    [imgView addSubview:lable];
}


- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{

    NSLog(@"didSelectAnnotationView");

    if ([view isKindOfClass:[FateMapAnnotationView class]]) {
        FateMapAnnotation* fateAnnotation = (FateMapAnnotation*)view.annotation;

        if (fateAnnotation.size > 1) {
            [mapView zoomIn];
        }
        [mapView setCenterCoordinate:view.annotation.coordinate];
     }
}

- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view {

  if ([view isKindOfClass:[FateMapAnnotationView class]]) {
        FateMapAnnotation* fateAnnotation = (FateMapAnnotation*)view.annotation;
        [mapView setCenterCoordinate:view.annotation.coordinate];
        if (fateAnnotation.size > 1) {
            [mapView zoomIn];
        }
    }
}

FateMapAnnotation

#import "BMKClusterItem.h"
#import "MapPointModel.h"
 /*
 *点聚合Annotation
 */
@interface FateMapAnnotation : BMKPointAnnotation

//所包含annotation个数
@property (nonatomic, assign) NSInteger size;

//@property (nonatomic,strong)MapPointModel *model;

@property(nonatomic, strong) NSString * title; //

@property (nonatomic,strong)BMKCluster *cluster;

FateMapAnnotationView

#import "BMKClusterItem.h"
#import "MapPointModel.h"
/*
 *自定义地图里面的标注
 *点聚合AnnotationView
 */
@interface FateMapAnnotationView : BMKAnnotationView
 //所包含annotation个数
@property (nonatomic, assign) NSInteger size;

@property (nonatomic,strong)BMKCluster *cluster;

@end

@implementation FateMapAnnotationView

@synthesize size = _size;


- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        [self setBounds:CGRectMake(0.f, 0.f, 82.f/2, 94.f/2)];
        [self addViews]; //自定义的View,随便你玩
    }
    return self;
}

@end
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容