iOS-百度地圖點聚合

一、坐標點轉(zhuǎn)換

坐標分為:平面坐標、球面坐標(地理經(jīng)緯度);在地圖應用上使用的平面坐標,如果拿到的是地理經(jīng)緯度坐標,那么是要進行轉(zhuǎn)換的

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

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

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

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

    //轉(zhuǎn)換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);
}

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

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

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]; //更新聚合狀態(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), ^{

              ///獲取聚合后的標注
              __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) { //坐標點沒有重疊的時候
                          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];
              });
          });
       }
   }

 }

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

- (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必須實現(xiàn)title屬性
    annotationView.canShowCallout = YES;

  //cluster.size 當前聚合狀態(tài)坐標系個數(shù),當一個的時候給它做一些你想要做的事情??
   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個數(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"
/*
 *自定義地圖里面的標注
 *點聚合AnnotationView
 */
@interface FateMapAnnotationView : BMKAnnotationView
 //所包含annotation個數(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
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容