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>)annotation47 {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 newAnnotationView.image
= [UIImage imageNamed:@"2"]; (图片显示的大小,可以通过newAnnotationView的frame设定) 如何修改气泡样式在气泡上默认是可以显示两行信息,一个title,一个subtitle,在此仅修改气泡的样式,如边框大小,字体,等基本信息,没有改变总体布局。  

 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 : UIView4 @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>)annotation26 {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>)overlay13 {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>)overlay25 {26     if ([overlay isKindOfClass:[BMKPolyline class]])27     {28         BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];29         polylineView.lineWidth = 5;30         polylineView.isFocus = YES;// 是否分段纹理绘制(突出显示),默认YES31         //加载分段纹理图片,必须否则不能进行分段纹理绘制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>)overlay24 {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>)overlay14 {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>)overlay14 {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>)overlay10 {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 }

时间: 2024-07-30 08:24:15

iOS百度地图-BMK标注&amp;覆盖物的相关文章

百度地图API标注+时间轴组合

百度地图API标注+时间轴组合 到新公司实习第八天,Boos让我结合百度地图api做一个动态展示标注变化的组件,要求地图展示某一天的标注,时间轴要求可以动态播放每一天的标注变化...然后我就开始coding... 准备工作: 申请百度api密钥(具体方法我也不多写了,大家应该都会) 了解一下百度地图API的开发指南和类参考文档(如果嫌麻烦的话 可以直接看Demo示例) 一.首先,先加载地图,你可以用实际的经纬度定位.浏览器定位.根据ip定位.根据城市名定位,这个你可以自己选择 // 创建Map实

iOS 百度地图获取地理位置名称

目前很多的应用都用到了百度地图这一版本,不过有些方法是我在网上并没有找到的,但是这并不意味着就没有相关的文章介绍,那么这里我所记录的就是百度地图获取地理位置,以及省份,城市,区域,其他的不多说了,直接进入正题吧:       创建地图基本图层 首先,需要到http://dev.baidu.com/wiki/imap/index.php?title=iOS平台/相关下载下载全部内容,包括文档,示例代码和开发包. 然后获取自己的API KEY,具体方法按百度的官网申请就行,比较简单. 下载的文件应该

IOS百度地图使用基础指南+原生分享&友盟分享

1.地图 1.获取用户的经纬度(CLLocationManager) 创建属性:CLLocationManager *mgr; 遵守协议:<CLLocationManagerDelegate> a>创建定位管理器 self.mgr = [[CLLocationManager alloc] init]; b>设置代理 self.mgr.delegate = self; c>开始定位 [self.mgr startUpdatingLocation]; 代理方法: -(void)l

iOS百度地图

一.百度地图sdk详细使用过程,参考下面的博客(我感觉很详细,而且有问题可以找楼主,他会给解答,感谢好心人-): 如何使用百度地图sdk,及相关demo下载 上述博客给的demo,是真机运行的(百度地图sdk区分真机和模拟器).博客中的demo,需要自己手动修改一些设置,才可运行(可对比下面的注意事项设置). 二.iOS中,使用百度地图sdk的注意事项: 1.百度地图sdk区分真机和模拟器,导入的时候需要注意: 2.工程中一定要有.mm文件: 3.一定要添加MapKit.framework.Co

百度地图上自定义图片覆盖物上加点击事件

在百度地图上加自定义图片覆盖物之后,在这个覆盖物上加点击手势,发现并不识别.原因不太清楚.不过可以通过别的方法来实现这个功能. 需要用到BMKMapViewDelegate中的方法: - (void)mapView:(BMKMapView *)mapView onClickedMapBlank:(CLLocationCoordinate2D)coordinate 然后判断所点击的坐标是否在指定区域内,在指定区域内之后,调用自己写的相应地方法即可. 百度地图上自定义图片覆盖物上加点击事件,布布扣,

iOS百度地图poi检索(基于百度地图2.3.0SDK开发)

正好做到百度地图的poi检索,拿来分享一下,基于百度地图2.3.0SDK开发,说实话,这个新版本poi真不好用 先来看头文件里面,定义poi对象,当然你也可以写到实现文件里面 #import <UIKit/UIKit.h> #import "BMapKit.h" @interface NearByPlaceViewController : ViewControllerBase <BMKPoiSearchDelegate> {     BMKPoiSearch *

ios 百度地图使用

第一步.引入 1.下载SDK  地址http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download 2.解压出BaiduMap_IOSSDK_v2.3.0_Lib.在工程中第三方中添加文件夹BaiDumap 然后在文件夹中放入(1:inc;2:mapapi.bundle:libbaidumapapi.a) 第二步.创建百度地图应用 1.http://lbsyun.baidu.com/apiconsole/key在这

IOS 百度地图导入最新 SDK 2.9 报错

IOS 百度地图导入最新2.9SDK 报错 _baidu_framework::HandleBasemapMerge(_baidu_vi::CVString const 解决: 因为XCode默认使用的C++标准库是苹果自己的libc++(LLVM C++ standard library with c++11 support),而百度地图SDK中使用的C++标准库是GNU C++的.解决方法,在target的build settings中搜索"c++ standard library"

iOS百度地图笔记(环境安装)

最近有项目要使用到地图功能,先做一下调研.百度的ios开发环境需要具备两点,一 秘钥 二 SDK.在百度上搜索“ios百度地图api”,然后打开网页“百度地图API-首页”.在此官网上可以获取所有需要的条件. 一 获取秘钥 在官网上点击“申请秘钥” 点击创建应用 这里需要注意几点: 1 应用类型选择 iOS SDK:2 安全码是使用百度地图SDK的APP的bundle ID. 点击提交,这样在你账号下的应用列表中就能看到百度给你分配的秘钥了.访问应用(AK)字段即为秘钥. 二 获取SDK并且配置