baidumap demo(一)

覆盖物概述

地图上自定义的标注点和覆盖物我们统称为地图覆盖物。您可以通过定制BMKAnnotation和BMKOverlay来添加对应的标注点和覆盖物。地图覆盖物的设计遵循数据与View分离的原则,BMKAnnotation和BMKOverlay系列的类主要用来存放覆盖物相关的数据,BMKAnnotaionView和BMKOverlayView系列类为覆盖物对应的View。

SDK支持画点、折线、圆、多边形(包括凹凸两种)、图片图层和自定义覆盖物。从2.0.0开始矢量地图采用OpenGL绘制,新增支持OpenGL绘制的基本线绘制、面绘制接口。详见AnnotationDemo,SDK内置的BMKPolylineOverlay、BMKPolygonOverlay,BMKCircleOverlay均采用OpenGL绘制。

添加标注

BMKAnnotation为标注对应的protocal,您可以自定义标注类实现该protocal。百度地图API也预置了基本的标注点——BMKPointAnnotation和一个大头针标注View——BMKPinAnnotationView,您可以直接使用来显示标注。示例如下: 
修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:

  1. #import <UIKit/UIKit.h>  
  2. #import "BMapKit.h"    
  3. @interface AnnotationDemoViewController : UIViewController <BMKMapViewDelegate> {   
  4.     IBOutlet BMKMapView* mapView;    
  5. }    
  6. @end    

修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForAnnotation:函数,并在viewDidAppear添加标注数据对象

  1. - (void) viewDidAppear:(BOOL)animated {    
  2.         // 添加一个PointAnnotation    
  3.         BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];    
  4.         CLLocationCoordinate2D coor;    
  5.         coor.latitude = 39.915;    
  6.         coor.longitude = 116.404;    
  7.         annotation.coordinate = coor;    
  8.         annotation.title = @"这里是北京";    
  9.         [mapView addAnnotation:annotation];    
  10.     }    
  11. // Override  
  12. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation  
  13.     {  
  14.         if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {  
  15.             BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];     
  16.             newAnnotationView.pinColor = BMKPinAnnotationColorPurple;     
  17.             newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示  
  18.             return newAnnotationView;     
  19.         }  
  20.         return nil;  
  21.     }  

运行后,会在地图显示对应的标注点,点击会弹出气泡,效果如图:
iosdev8.jpg

删除标注

通过removeAnnotation:函数实现对已添加标注的删除功能,示例如下:

  1. if (annotation != nil) {    
  2.     [_mapView removeAnnotation:annotation];    
  3. }  

添加折线

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:

  1. #import <UIKit/UIKit.h>  
  2. #import "BMapKit.h"  
  3. @interface OverlayDemoViewController : UIViewController <BMKMapViewDelegate>{  
  4.     IBOutlet BMKMapView* mapView;  
  5. }  
  6. @end  

修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加折线数据对象:

  1. - (void)viewDidLoad {     
  2.         [super viewDidLoad];    
  3.         // 添加折线覆盖物    
  4.         CLLocationCoordinate2D coors[2] = {0};    
  5.         coors[0].latitude = 39.315;    
  6.         coors[0].longitude = 116.304;    
  7.         coors[1].latitude = 39.515;    
  8.         coors[1].longitude = 116.504;    
  9.     BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];    
  10.     [_mapView addOverlay:polyline];    
  11. }    
  12. // Override    
  13. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{    
  14.    if ([overlay isKindOfClass:[BMKPolyline class]]){    
  15.         BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease];    
  16.         polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];   
  17.         polylineView.lineWidth = 5.0;    
  18.         return polylineView;    
  19.    }    
  20.    return nil;    
  21. }    

运行后,效果如图:iosdev9.jpg

添加弧线

百度地图iOS SDK自v2.1.1版本起,新增了绘制弧线的方法。用户可以根据三个有序点唯一确定一条弧线,满足您的业务需求。首先,修改您的.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加弧线数据对象:

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3. //添加弧线覆盖物  
  4. //传入的坐标顺序为起点、途经点、终点  
  5. CLLocationCoordinate2D coords[3] = {0};  
  6. coords[0].latitude = 39.9374;  
  7. coords[0].longitude = 116.350;  
  8. coords[1].latitude = 39.9170;  
  9. coords[1].longitude = 116.360;  
  10. coords[2].latitude = 39.9479;  
  11. coords[2].longitude = 116.373;  
  12. BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords];  
  13.    [_mapView addOverlay:arcline];  
  14.   
  15. }  
  16. //根据overlay生成对应的View  
  17. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<bmkoverlay>)overlay  
  18. {  
  19. if ([overlay isKindOfClass:[BMKArcline class]])  
  20.     {  
  21.         BMKArclineView* arclineView = [[[BMKArclineView alloc] initWithOverlay:overlay] autorelease];  
  22.         arclineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];  
  23.         arclineView.lineWidth = 5.0;  
  24.         return arclineView;  
  25.         }  
  26.     return nil;  
  27. }  
  28.   
  29. </bmkoverlay>  

运行后,效果如图:iosdev9.jpg

添加多边形

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议: 
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加多边形数据对象:

  1. - (void)viewDidLoad {     
  2.     [super viewDidLoad];     
  3.     // 添加多边形覆盖物    
  4.     CLLocationCoordinate2D coords[3] = {0};    
  5.     coords[0].latitude = 39;    
  6.     coords[0].longitude = 116;    
  7.     coords[1].latitude = 38;    
  8.     coords[1].longitude = 115;    
  9.     coords[2].latitude = 38;    
  10.     coords[2].longitude = 117;    
  11.     BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:3];    
  12.     [mapView addOverlay:polygon];    
  13. }    
  14. // Override    
  15. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{  
  16.     if ([overlay isKindOfClass:[BMKPolygon class]]){    
  17.         BMKPolygonView* polygonView = [[[BMKPolygonView alloc] initWithOverlay:overlay] autorelease];    
  18.         polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];    
  19.         polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];    
  20.         polygonView.lineWidth = 5.0;    
  21.         return polygonView;    
  22.     }    
  23.     return nil;  
  24. }  

运行后,效果如图:iosdev10.jpg

添加圆

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议: 
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加圆数据对象:

  1. - (void)viewDidLoad {     
  2.     [super viewDidLoad];    
  3.     // 添加圆形覆盖物    
  4.     CLLocationCoordinate2D coor;    
  5.     coor.latitude = 39.915;    
  6.     coor.longitude = 116.404;    
  7.     BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000];    
  8.     [_mapView addOverlay:circle];    
  9. }    
  10. // Override    
  11. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{    
  12.     if ([overlay isKindOfClass:[BMKCircle class]]){    
  13.         BMKCircleView* circleView = [[[BMKCircleView alloc] initWithOverlay:overlay] autorelease];    
  14.         circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];    
  15.         circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];    
  16.         circleView.lineWidth = 10.0;    
  17.         return circleView;    
  18.     }    
  19.     return nil;   
  20. }  

运行后,效果如图:iosdev11.jpg

添加图片图层

自V2.1.0开始,新增图片图层,开发者可在地图的指定位置上添加图片。该图片可随地图的平移、缩放、旋转等操作做相应的变换。图片图层是一种特殊的Overlay, 它位于底图和底图标注层之间(即图片图层不会遮挡地图标注信息), 此外,图片图层的添加顺序不会影响其他图层(例如:POI搜索图层、我的位置图层等)的叠加关系。

图片图层对象初始化的方法有两种:1)根据指定经纬度坐标生成 2)根据指定区域生成。下面举例分步说明添加图片图层的步骤:

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:

  1. @interface AnnotationDemoViewController : UIViewController<BMKMapViewDelegate>{  
  2.       IBOutlet BMKMapView* _mapView;  
  3. }  
  4. @end  
  5.    

修改您的ViewController.m文件,在viewDidLoad添加图片图层对象:

  1. - (void)viewDidLoad {  
  2.         [super viewDidLoad];  
  3.         //添加图片图层覆盖物(第一种:根据指定经纬度坐标生成)  
  4.         CLLocationCoordinate2D coors;  
  5.             coors.latitude = 39.800;  
  6.         coors.longitude = 116.404;  
  7.         BMKGroundOverlay* ground = [BMKGroundOverlay groundOverlayWithPosition:coors   
  8.             zoomLevel:11 anchor:CGPointMake(0.0f,0.0f)   
  9.             icon:[UIImage imageWithName:@"test.png"]];  
  10.         [_mapView addOverlay:ground];  
  11.         //添加图片图层覆盖物(第二种:根据指定区域生成)  
  12.         CLLocationCoordinate2Dcoords[2] = {0};  
  13.         coords[0].latitude = 39.815;  
  14.         coords[0].longitude = 116.404;  
  15.         coords[1].latitude = 39.915;  
  16.         coords[1].longitude = 116.504;  
  17.         BMKCoordinateBounds bound;  
  18.         bound.southWest = coords[0];  
  19.         bound.northEast = coords[1];  
  20.         BMKGroundOverlay* ground2 = [BMKGroundOverlay groundOverlayWithBounds: bound  
  21.             icon:[UIImage imageWithName:@"test.png"]];  
  22.         [_mapView addOverlay:ground2];  
  23. }  

修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数:

  1. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay{  
  2.         if ([overlay isKindOfClass:[BMKGroundOverlay class]]){  
  3.             BMKGroundOverlayView* groundView = [[[BMKGroundOverlayView alloc] initWithOverlay:overlay] autorelease];  
  4.             return groundView;  
  5.         }  
  6.         return nil;  
  7. }  

运行后,效果如图:

sdkioslayer1.pngsdkioslayer2.png

2D效果图3D效果图

 

添加自定义Overlay

从2.0.0开始,地图渲染采用OpenGL方式实现,因此覆盖物基类BMKOverlayView新增glRender接口,以及绘制基本线renderLinesWithPoints、面renderRegionWithPoints的接口来实现对覆盖物的OpenGL渲染。绘制自定义overlay时,继承BMKOverlayView的子类需实现glRender接口,在glRender中通过调用renderLinesWithPoints、renderRegionWithPoints来组合自己想要实现的图形,示例代码如下,详细示例请参见CustomOverlayDemoViewController:

CustomOverlayView继承BMKOverlayPathView,在CustomOverlayView中实现glRender

  1. - (void)glRender  {     
  2.     //自定义overlay绘制    
  3.     CustomOverlay *customOverlay = [self customOverlay];    
  4.     if (customOverlay.pointCount >= 3) {    
  5.        [self renderRegionWithPoints:customOverlay.points pointCount:customOverlay.pointCount fillColor:self.fillColor usingTriangleFan:YES];//绘制多边形    
  6.     }else    
  7.     {    
  8.         [self renderLinesWithPoints:customOverlay.points pointCount:customOverlay.pointCount strokeColor:self.strokeColor lineWidth:self.lineWidth looped:NO];//绘制线    
  9.     }    
  10. }    

如果不实现glRender,则需实现drawMapRect默认使用系统GDI绘制,GDI绘制方式在overlayView尺寸较大时可能有效率问题,因此建议使用glRender来实现自定义overlay绘制。

删除Overlay

通过removeOverlay:函数实现对已添加标注的删除功能,示例如下:

  1. if (overlay != nil) {    
  2.     [_mapView removeOverlay:overlay];    
  3. }    
原文地址:https://www.cnblogs.com/yulang314/p/3549744.html