iOS-BMK标注覆盖物
在iOS开发中,地图算是一个比较重要的模块。我们常用的地图有高德地图,百度地图,谷歌地图,对于中国而言,苹果公司已经不再使用谷歌地图,官方使用的是高德地图。下面将讲述一下百度地图开发过程中的一些小的知识点。
对于如何配置百度地图的开发环境,在此不再讲述,具体可以参考:http://developer.baidu.com/map/index.php?title=iossdk/guide/buildproject
百度地图iOS的API下载地址:http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download
关于百度地图的基本使用,我们可以参考百度地图的开发文档,在此主要总结一下开发文档中一些重要的知识点和延伸点。(地图版本IOS SDK 2.9.0)
首先说明一下百度地图开发中可能遇到的问题:
如何添加标注(系统标注和自定义标注)
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4 /* 5 for (int i = 0; i < 3; i++) { 6 BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init]; 7 CLLocationCoordinate2D coor; 8 coor.latitude = 39.915 + i*2; 9 coor.longitude = 116.404 + i*2; 10 annotation.coordinate = coor; 11 annotation.title = @"这里是北京"; 12 [myMapView addAnnotation:annotation]; 13 } 14 */ 15 BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init]; 16 CLLocationCoordinate2D coor; 17 coor.latitude = 39.915; 18 coor.longitude = 116.404; 19 annotation.coordinate = coor; 20 annotation.title = @"这里是北京"; 21 annotation.subtitle = @"1"; 22 //[myMapView addAnnotation:annotation]; 23 24 BMKPointAnnotation* annotation1 = [[BMKPointAnnotation alloc]init]; 25 CLLocationCoordinate2D coor1; 26 coor1.latitude = 38.915; 27 coor1.longitude = 113.404 + 2; 28 annotation1.coordinate = coor1; 29 annotation1.title = @"这里也是北京"; 30 annotation1.subtitle = @"2"; 31 //[myMapView addAnnotation:annotation1]; 32 33 BMKPointAnnotation* annotation2 = [[BMKPointAnnotation alloc]init]; 34 CLLocationCoordinate2D coor2; 35 coor2.latitude = 38.915; 36 coor2.longitude = 119.404 + 2; 37 annotation2.coordinate = coor2; 38 annotation2.title = @"这里同样是北京"; 39 annotation2.subtitle = @"3"; 40 //[myMapView addAnnotation:annotation2]; 41 42 NSArray *arr = [NSArray arrayWithObjects:annotation,annotation1,annotation2, nil]; 43 [myMapView addAnnotations:arr]; 44 } 45 46 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation 47 { 48 if ([annotation isKindOfClass:[BMKPointAnnotation class]]) 49 { 50 BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"]; 51 newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"]; 52 newAnnotationView.pinColor = BMKPinAnnotationColorPurple; 53 newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 54 return newAnnotationView; 55 } 56 return nil; 57 }
系统标记
1 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation 2 { 3 if ([annotation isKindOfClass:[BMKPointAnnotation class]]) 4 { 5 BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"]; 6 newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"]; 7 newAnnotationView.pinColor = BMKPinAnnotationColorPurple; 8 newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 9 10 UIView *view = [[UIView alloc]init]; 11 view.frame = CGRectMake(0, 0, 100, 60); 12 view.backgroundColor = [UIColor greenColor]; 13 [view.layer setMasksToBounds:YES]; 14 [view.layer setCornerRadius:3]; 15 view.alpha = 0.9; 16 17 UILabel *label1 = [[UILabel alloc]init]; 18 label1.frame = CGRectMake(0, 0, 100, 30); 19 label1.text = annotation.title; 20 label1.numberOfLines = 0; 21 label1.font = [UIFont systemFontOfSize:12]; 22 label1.textColor = [UIColor blackColor]; 23 [view addSubview:label1]; 24 25 UILabel *label2 = [[UILabel alloc]init]; 26 label2.frame = CGRectMake(0, 30, 100, 30); 27 label2.text = annotation.subtitle; 28 label2.numberOfLines = 0; 29 label2.font = [UIFont systemFontOfSize:10]; 30 label2.textColor = [UIColor lightGrayColor]; 31 [view addSubview:label2]; 32 33 BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:view]; 34 pView.frame = CGRectMake(0, 0, 100, 60); 35 ((BMKPinAnnotationView *)newAnnotationView).paopaoView = pView; 36 return newAnnotationView; 37 } 38 return nil; 39 }
简单定义气泡样式
如果我们想要修改气泡的布局怎样处理呢?
因为系统默认的一个是坐标,一个标题,一个子标题,我们想要按照自己的方式布局弹出的气泡,我们需要做什么工作呢?
首先系统提供BMKPointAnnotation的方法不够我们使用,我们需要继承这个类,假如新类为myPoint,添加一些新的属性。比如这个类,我们需要添加三个属性。
分别是NSString *imgName ; NSString *placeName; NSString *idNum;
接下来我们我们需要自定义气泡,气泡本身是一个UIView,我们可以在继承于UIView,创建一个子类myPaopao;在myPaopao里面要添加三个控件,显示上面定义的三个属性。
1 #import <UIKit/UIKit.h> 2 3 @interface myPaopao : UIView 4 @property(nonatomic,retain)UIImageView *imgView; 5 @property(nonatomic,retain) UILabel *placeName; 6 @property(nonatomic,retain) UILabel *idNum; 7 8 @end
myPaopao.h
1 #import "myPaopao.h" 2 3 @implementation myPaopao 4 5 -(instancetype)initWithFrame:(CGRect)frame 6 { 7 self = [super initWithFrame:frame]; 8 if (self) 9 { 10 self.frame = CGRectMake(0, 0, 150, 60); 11 self.backgroundColor = [UIColor whiteColor]; 12 13 _imgView = [[UIImageView alloc]init]; 14 _imgView.frame = CGRectMake(0, 0, 60, 60); 15 [self addSubview:_imgView]; 16 17 _placeName = [[UILabel alloc]init]; 18 _placeName.frame = CGRectMake(60, 0, 90, 30); 19 _placeName.font = [UIFont systemFontOfSize:12]; 20 [self addSubview:_placeName]; 21 22 _idNum = [[UILabel alloc]init]; 23 _idNum.frame = CGRectMake(60, 30, 90, 30); 24 [self addSubview:_idNum]; 25 26 } 27 return self; 28 } 29 @end
myPaopao.m
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4 myPoint* annotation = [[myPoint alloc]init]; 5 CLLocationCoordinate2D coor; 6 coor.latitude = 39.915; 7 coor.longitude = 116.404; 8 annotation.coordinate = coor; 9 annotation.imgName = @"1.jpg"; 10 annotation.placeName = @"这里是北京"; 11 annotation.idNum = @"1"; 12 [myMapView addAnnotation:annotation]; 13 14 myPoint* annotation1 = [[myPoint alloc]init]; 15 CLLocationCoordinate2D coor1; 16 coor1.latitude = 38.915; 17 coor1.longitude = 113.404 + 2; 18 annotation1.coordinate = coor1; 19 annotation1.imgName = @"2.jpg"; 20 annotation1.placeName = @"这里也是北京"; 21 annotation1.idNum = @"2"; 22 [myMapView addAnnotation:annotation1]; 23 24 } 25 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation 26 { 27 if ([annotation isKindOfClass:[myPoint class]]) 28 { 29 myPoint *myAnnotation = (myPoint *)annotation; 30 31 BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"]; 32 newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"]; 33 newAnnotationView.pinColor = BMKPinAnnotationColorPurple; 34 newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 35 myPaopao *paopapo = [[myPaopao alloc]init]; 36 37 paopapo.imgView.image = [UIImage imageNamed:myAnnotation.imgName]; 38 paopapo.placeName.text = myAnnotation.placeName; 39 paopapo.idNum.text = myAnnotation.idNum; 40 BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:paopapo]; 41 ((BMKPinAnnotationView *)newAnnotationView).paopaoView = pView; 42 return newAnnotationView; 43 } 44 return nil; 45 }
实现代码
如果有需要,我们还可以添加一个按钮,跳转到详情界面,添加按钮的方法,与上面的方法相同,在此
点聚合功能
点聚合功能是v2.9.0新增加的一个功能,如果在一个区域有大量的点,会产生覆盖现象,点聚合功能可以实现将很多点聚合到一个点上,通过缩放比例,可以显示更多点或聚合点。我们在下载SDK的时候,会带有一个Demo,从Demo中我们可以找到相应的实现代码。在开发文档中给出了我们核心代码:
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4 CLLocationCoordinate2D coors[2] = {0}; 5 coors[0].latitude = 39.315; 6 coors[0].longitude = 116.304; 7 coors[1].latitude = 30.515; 8 coors[1].longitude = 116.504; 9 BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coors count:2]; 10 [myMapView addOverlay:polyline]; 11 } 12 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 13 { 14 NSLog(@"sdf"); 15 if ([overlay isKindOfClass:[BMKPolyline class]]) 16 { 17 BMKPolylineView *polylineView = [[BMKPolylineView alloc]initWithPolyline:overlay]; 18 polylineView.strokeColor = [[UIColor greenColor]colorWithAlphaComponent:1]; 19 polylineView.lineWidth = 5.0; 20 return polylineView; 21 } 22 return nil; 23 }
折线
分段纹理分段折线
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4 CLLocationCoordinate2D coords[5] = {0}; 5 coords[0].latitude = 39.965; 6 coords[0].longitude = 116.404; 7 coords[1].latitude = 39.925; 8 coords[1].longitude = 116.454; 9 coords[2].latitude = 39.955; 10 coords[2].longitude = 116.494; 11 coords[3].latitude = 39.905; 12 coords[3].longitude = 116.654; 13 coords[4].latitude = 39.965; 14 coords[4].longitude = 116.704; 15 //构建分段文理索引数组 16 NSArray *textureIndex = [NSArray arrayWithObjects: 17 [NSNumber numberWithInt:0], 18 [NSNumber numberWithInt:1], 19 [NSNumber numberWithInt:2], 20 [NSNumber numberWithInt:1], nil]; 21 BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:textureIndex]; 22 [myMapView addOverlay:polyline]; 23 } 24 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 25 { 26 if ([overlay isKindOfClass:[BMKPolyline class]]) 27 { 28 BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay]; 29 polylineView.lineWidth = 5; 30 polylineView.isFocus = YES;// 是否分段纹理绘制(突出显示),默认YES 31 //加载分段纹理图片,必须否则不能进行分段纹理绘制 32 [polylineView loadStrokeTextureImages: 33 [NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"], 34 [UIImage imageNamed:@"2.jpg"], 35 [UIImage imageNamed:@"3.jpg"],nil]]; 36 return polylineView; 37 } 38 return nil; 39 }
纹理折线
分段颜色分段折线
代码段:
1 -(void)viewDidAppear:(BOOL)animated 2 { 3 CLLocationCoordinate2D coords[5] = {0}; 4 coords[0].latitude = 39.965; 5 coords[0].longitude = 116.404; 6 coords[1].latitude = 39.925; 7 coords[1].longitude = 116.454; 8 coords[2].latitude = 39.955; 9 coords[2].longitude = 116.494; 10 coords[3].latitude = 39.905; 11 coords[3].longitude = 116.654; 12 coords[4].latitude = 39.965; 13 coords[4].longitude = 116.704; 14 //构建分段文理索引数组 15 NSArray *colorIndexs = [NSArray arrayWithObjects: 16 [NSNumber numberWithInt:0], 17 [NSNumber numberWithInt:1], 18 [NSNumber numberWithInt:2], 19 [NSNumber numberWithInt:1], nil]; 20 BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:colorIndexs]; 21 [myMapView addOverlay:polyline]; 22 } 23 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 24 { 25 if ([overlay isKindOfClass:[BMKPolyline class]]) 26 { 27 BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay]; 28 polylineView.lineWidth = 5; 29 // 使用分段颜色绘制时,必须设置(内容必须为UIColor) 30 polylineView.colors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor redColor], [UIColor yellowColor], nil]; 31 return polylineView; 32 } 33 return nil; 34 }
颜色分段
弧线(起点,途经点,终点)
代码段:
1 -(void)viewDidAppear:(BOOL)animated 2 { 3 CLLocationCoordinate2D coords[3] = {0}; 4 coords[0].latitude = 39.9374; 5 coords[0].longitude = 116.350; 6 coords[1].latitude = 39.9170; 7 coords[1].longitude = 116.360; 8 coords[2].latitude = 39.9479; 9 coords[2].longitude = 116.373; 10 BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords]; 11 [myMapView addOverlay:arcline]; 12 } 13 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 14 { 15 if ([overlay isKindOfClass:[BMKArcline class]]) 16 { 17 NSLog(@"adf"); 18 BMKArclineView* arclineView = [[BMKArclineView alloc] initWithOverlay:overlay]; 19 arclineView.strokeColor = [[UIColor blackColor]colorWithAlphaComponent:0.5]; 20 arclineView.lineWidth = 5.0; 21 return arclineView; 22 } 23 return nil; 24 }
弧线
多边形
代码段:
1 -(void)viewDidAppear:(BOOL)animated 2 { 3 CLLocationCoordinate2D coords[3] = {0}; 4 coords[0].latitude = 39; 5 coords[0].longitude = 116; 6 coords[1].latitude = 38; 7 coords[1].longitude = 115; 8 coords[2].latitude = 38; 9 coords[2].longitude = 117; 10 BMKPolygon *ploygon = [BMKPolygon polygonWithCoordinates:coords count:3]; 11 [myMapView addOverlay:ploygon]; 12 } 13 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 14 { 15 if ([overlay isKindOfClass:[BMKPolygon class]]) 16 { 17 BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay]; 18 polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1]; 19 polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2]; 20 polygonView.lineWidth = 5.0; 21 22 return polygonView; 23 } 24 return nil; 25 }
多边形
圆
代码段:
1 -(void)viewDidAppear:(BOOL)animated 2 { 3 CLLocationCoordinate2D coor; 4 coor.latitude = 39.915; 5 coor.longitude = 116.404; 6 BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000]; 7 [myMapView addOverlay:circle]; 8 } 9 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 10 { 11 if ([overlay isKindOfClass:[BMKCircle class]]) 12 { 13 BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay]; 14 circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5]; 15 circleView.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5]; 16 circleView.lineWidth = 10.0; 17 18 return circleView; 19 } 20 return nil; 21 }
圆
图片图层
第一种方法根据坐标,缩放尺度确定
代码段:
1 -(void)viewDidAppear:(BOOL)animated 2 { 3 CLLocationCoordinate2D coors; 4 coors.latitude = 39.800; 5 coors.longitude = 116.404; 6 BMKGroundOverlay* ground = [BMKGroundOverlay groundOverlayWithPosition:coors 7 zoomLevel:5 anchor:CGPointMake(0.0f,0.0f) 8 icon:[UIImage imageNamed:@"1.jpg"]]; 9 [myMapView addOverlay:ground]; 10 } 11 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 12 { 13 if ([overlay isKindOfClass:[BMKGroundOverlay class]]){ 14 BMKGroundOverlayView* groundView = [[BMKGroundOverlayView alloc] initWithOverlay:overlay]; 15 return groundView; 16 } 17 return nil; 18 }
图片图层一
第二种方法根据指定区域生成
代码段:
1 -(void)viewDidAppear:(BOOL)animated 2 { 3 CLLocationCoordinate2D coords[2] = {0}; 4 coords[0].latitude = 39.815; 5 coords[0].longitude = 116.404; 6 coords[1].latitude = 39.915; 7 coords[1].longitude = 116.504; 8 BMKCoordinateBounds bound; 9 bound.southWest = coords[0]; 10 bound.northEast = coords[1]; 11 BMKGroundOverlay* ground = [BMKGroundOverlay groundOverlayWithBounds: bound 12 icon:[UIImage imageNamed:@"2.jpg"]]; 13 [myMapView addOverlay:ground]; 14 } 15 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay 16 { 17 if ([overlay isKindOfClass:[BMKGroundOverlay class]]){ 18 BMKGroundOverlayView* groundView = [[BMKGroundOverlayView alloc] initWithOverlay:overlay]; 19 return groundView; 20 } 21 return nil; 22 }
图片图层二
转载于:https://www.cnblogs.com/wangyaoguo/p/4886993.html
iOS-BMK标注覆盖物相关推荐
- android 百度地图标注覆盖物学习
介绍 最近着手做一个新的项目,其中涉及到地图定位,以及从服务器获取附近的特定商家并在地图上面标注出来.于是就研究了一下百度地图标注覆盖物功能,然后进行一点个人总结,以便学习交流使用,个人学习总结还请各 ...
- 百度地图API详解之地图标注覆盖物
本文将向大家介绍百度地图API的标注(Marker)的使用方法和一些实现细节. 标注概述 标注(Marker)是用来表示一个点位置的可见元素,每个标注自身都包含地理信息.比如你在西单商场位置添加了一个 ...
- 百度地图最新版本sdk使用系列(四)-开启卫星地图,实时交通图,实现标注覆盖物等功能
废话不多说,详细看注释,具体代码如下: "` Public class MainActivity extends Activity { private static final String ...
- 百度地图覆盖物OverlayOptions
转载:http://blog.csdn.net/u012637501/article/details/45603857 目前百度地图SDK所提供的地图等级为3-19级,所包含的信息有建筑物.道路.河流 ...
- 百度地图--展示行政区划(省市区县)和添加多个标注点案例
百度地图JavaScript API支持HTTP和HTTPS,免费对外开放,可直接使用.接口使用无次数限制.在使用前,您需先申请密钥(ak)才可使用. 申请密钥链接: http://lbsyun.ba ...
- 第六章 第二节:AndroidStudio地图覆盖物
文章目录 覆盖物介绍 添加覆盖物的步骤 OverlayOptions 坐标点 覆盖物类型 addOverlay()方法 标注覆盖物 标注覆盖物选项类 标注覆盖物工厂类 标注覆盖物设置监听器 代码1 显 ...
- 百度地图开发(二)之添加覆盖物 地理编码和反地理编码
转载请注明出处: http://blog.csdn.net/crazy1235/article/details/43377545 之前写过一篇关于百度地图开发的blog,主要介绍了百度地图的基本地图的 ...
- android与ios ui切图关系,APP-IOS与Android界面设计与切图
做一全套的APP设计,流程是: 1.界面设计:设计IOS界面:设计Android界面. 2.切图:切IOS的2倍图和3倍图:切Android的hdpi,xhdpi,xxhdpi这三个尺寸的图. 3.标 ...
- 地图与定位(五)高德地图服务一
序言: 高德地图是一款经常在苹果手机使用的地图平台.在国内iOS的地图服务都是由高德提供的.在上一节中,我们介绍了一下如何使用MapKit框架实现地图服务,以及如何调用了系统内置的地图应用.除了使用i ...
最新文章
- HTML5 Canvas 基础API和实例
- CMD 一条命令 执行 多条命令
- Java消息服务JMS详解
- 随机对照试验设计的原理
- PRML-系列二之2.3
- C# Wpf异步修改UI,多线程修改UI(二)
- 索尼Xperia 2带壳渲染图曝光:外形依然很索尼
- Android代码优化
- 网页读不出php语句,php - phpmyadmin显示代码而不是网页 - 堆栈内存溢出
- I2S原理分析(二十九)
- Unity3d iOS 内购详细流程总汇
- 2019.9.在循环队列中设置一个标志flag,当front=rear且flag=0时为队空,当front=rear且flag=1时候队满,请编写相应的入队和出队算法(假设队头指针front指向对头.
- Invalid topo name mytopo
- 重疾险对比:“瑞泰瑞享安康” V.S. “信泰百万无忧”
- 亚马逊多账号关联原因
- Clonezilla 再生龙
- 怎样提高计算机内存,怎样增加电脑虚拟内存
- JAVA开发(第三方接口授权访问)
- ST-Link设备连接。 Could not verify ST device! Abort connection.
- 数据大屏领导驾驶舱大数据分析UI1-4(PSD-持续更新)
热门文章
- java怎么打增量包_eclipse实现JavaWeb应用增量打包
- Vue移动端项目——字体图标的使用
- LeetCode 1760. 袋子里最少数目的球(二分查找)
- jieba分词提取小说人名
- LeetCode 978. 最长湍流子数组(DP)
- LeetCode 980. 不同路径 III(DFS+回溯)
- 计算机软件无形资产机械工具,考前秘籍,无形资产的定义及确认
- antd的 input有下拉_解决antd 下拉框 input [defaultValue] 的值的问题
- Python基础(五)--函数
- 计算机简单故障时的排除方法,电脑简单故障排除解决办法大全