一、坐标点转换
坐标分为:平面坐标、球面坐标(地理经纬度);在地图应用上使用的平面坐标,如果拿到的是地理经纬度坐标,那么是要进行转换的
-(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