iOS-百度地圖點(diǎn)聚合

一约啊、坐標(biāo)點(diǎn)轉(zhuǎn)換

坐標(biāo)分為:平面坐標(biāo)邑遏、球面坐標(biāo)(地理經(jīng)緯度);在地圖應(yīng)用上使用的平面坐標(biāo)恰矩,如果拿到的是地理經(jīng)緯度坐標(biāo)记盒,那么是要進(jìn)行轉(zhuǎn)換的

-(void)Convert{
            
      //北緯N22°48′26.53″  東經(jīng)E113°33′47.24″ ,拆分為:度 22  、分48外傅、秒26.53 
      /*----先進(jìn)行六十進(jìn)制轉(zhuǎn)換 WGS84坐標(biāo)----*/
      double  s = 26.53/60; 
      double  f = ( 48 + s )/60;
      double  lat = 22 + f;

  //這時(shí)候 c 是 WGS84:為一種大地坐標(biāo)系纪吮,也是目前廣泛使用的GPS全球衛(wèi)星定位系統(tǒng)使用的坐標(biāo)系;
 //百度地圖SDK在國內(nèi)(包括港澳臺)使用的是BD09坐標(biāo)萎胰;在海外地區(qū)碾盟,統(tǒng)一使用WGS84坐標(biāo)

    /*----BD09坐標(biāo)轉(zhuǎn)換----*/
    
    //下面是百度官方提供的轉(zhuǎn)換,每應(yīng)用提供轉(zhuǎn)換是不一樣的技竟,雖然是同一個(gè)地方冰肴,但是轉(zhuǎn)換后的坐標(biāo)點(diǎn)是不一樣的,這里轉(zhuǎn)換后的坐標(biāo)點(diǎn)只適用百度地圖
   CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(lat,lon);//原始坐標(biāo)

    //轉(zhuǎn)換國測局坐標(biāo)(google地圖、soso地圖熙尉、aliyun地圖联逻、mapabc地圖和amap地圖所用坐標(biāo))至百度坐標(biāo)
   NSDictionary* testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_COMMON);

    //轉(zhuǎn)換WGS84坐標(biāo)至百度坐標(biāo)(加密后的坐標(biāo))
    testdic = BMKConvertBaiduCoorFrom(coor,BMK_COORDTYPE_GPS);
    
    //解密加密后的坐標(biāo)字典
    CLLocationCoordinate2D baiduCoor = BMKCoorDictionaryDecode(testdic);

    double y = baiduCoor.latitude;
    double x = baiduCoor.longitude;
    NSLog(@"百度地圖BD09坐標(biāo) --- %f,%f",y,x);
}

二、添加坐標(biāo)點(diǎn)检痰、數(shù)據(jù)

-(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:@"坐標(biāo):%d",I];
          [array_description addObject:model];
      }
      [self addPointJuheWithCoorArray:array_description];  //添加模型數(shù)組
}

三包归、添加模型數(shù)組

NSMutableArray *_clusterCaches;//點(diǎn)聚合緩存標(biāo)注
BMKClusterManager *_clusterManager;//點(diǎn)聚合管理類

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

  _clusterCaches = [[NSMutableArray alloc] init];
  for (NSInteger i = 3; i < 22; i++) {
        [_clusterCaches addObject:[NSMutableArray array]];
  }
  //點(diǎn)聚合管理類
  _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]; //更新聚合狀態(tài)

}

四、更新聚合狀態(tài)

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), ^{

              ///獲取聚合后的標(biāo)注
              __block NSArray *array = [self->_clusterManager getClusters:self->_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;
           
                      if (item.size == 1) { //坐標(biāo)點(diǎn)沒有重疊的時(shí)候
                          BMKClusterItem *clusterItem = item.clusterItems[0];
                          MapPointModel * model = clusterItem.model;
                          annotation.title = model.title;
                       }
                        annotation.title = [NSString stringWithFormat:@"我是%lu個(gè)", (unsigned long)item.size];
                        [clusters addObject:annotation];
                   }
                  [self->_mapView removeAnnotations:self->_mapView.annotations];
                  [self->_mapView addAnnotations:clusters];
              });
          });
       }
   }

 }

//地圖改變的時(shí)候發(fā)送請求

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


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

    NSString *AnnotationViewID = @"xidanMark";

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

    // 緩存沒有命中攀细,自己構(gòu)造一個(gè)箫踩,一般首次添加annotation代碼會運(yùn)行到此處
    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必須實(shí)現(xiàn)title屬性
    annotationView.canShowCallout = YES;

  //cluster.size 當(dāng)前聚合狀態(tài)坐標(biāo)系個(gè)數(shù)谭贪,當(dāng)一個(gè)的時(shí)候給它做一些你想要做的事情??
   if (cluster.size == 1 ) { //其中一個(gè)點(diǎn)只有一個(gè)坐標(biāo)境钟,添加點(diǎn)擊彈出的泡泡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"
 /*
 *點(diǎn)聚合Annotation
 */
@interface FateMapAnnotation : BMKPointAnnotation

//所包含annotation個(gè)數(shù)
@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"
/*
 *自定義地圖里面的標(biāo)注
 *點(diǎn)聚合AnnotationView
 */
@interface FateMapAnnotationView : BMKAnnotationView
 //所包含annotation個(gè)數(shù)
@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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末俭识,一起剝皮案震驚了整個(gè)濱河市慨削,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌套媚,老刑警劉巖缚态,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異堤瘤,居然都是意外死亡玫芦,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門本辐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來桥帆,“玉大人,你說我怎么就攤上這事慎皱±铣妫” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵茫多,是天一觀的道長祈匙。 經(jīng)常有香客問我,道長天揖,這世上最難降的妖魔是什么夺欲? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮今膊,結(jié)果婚禮上洁闰,老公的妹妹穿的比我還像新娘。我一直安慰自己万细,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著赖钞,像睡著了一般腰素。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上雪营,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天弓千,我揣著相機(jī)與錄音,去河邊找鬼献起。 笑死洋访,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的谴餐。 我是一名探鬼主播姻政,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼岂嗓!你這毒婦竟也來了汁展?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤厌殉,失蹤者是張志新(化名)和其女友劉穎食绿,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體公罕,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡器紧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了楼眷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铲汪。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖摩桶,靈堂內(nèi)的尸體忽然破棺而出桥状,到底是詐尸還是另有隱情,我是刑警寧澤硝清,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布辅斟,位于F島的核電站,受9級特大地震影響芦拿,放射性物質(zhì)發(fā)生泄漏士飒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一蔗崎、第九天 我趴在偏房一處隱蔽的房頂上張望酵幕。 院中可真熱鬧,春花似錦缓苛、人聲如沸芳撒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽笔刹。三九已至芥备,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間舌菜,已是汗流浹背萌壳。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留日月,地道東北人袱瓮。 一個(gè)月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像爱咬,于是被迫代替她去往敵國和親尺借。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容