一约啊、坐標(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