地图覆盖物
覆盖物概述
地图上自定义的标注点和覆盖物我们统称为地图覆盖物。您可以通过定制BMKAnnotation和BMKOverlay来添加对应的标注点和覆盖物。地图覆盖物的设计遵循数据与View分离的原则,BMKAnnotation和BMKOverlay系列的类主要用来存放覆盖物相关的数据,BMKAnnotaionView和BMKOverlayView系列类为覆盖物对应的View。
SDK支持画点、折线、圆、多边形、自定义覆盖物。从2.0.0开始矢量地图采用OpenGL绘制,新增支持OpenGL绘制的基本线绘制、面绘制接口。详见demo,SDK内置的BMKPolylineOverlay、BMKPolygenOverlay,BMKCircleOverlay采用OpenGL绘制。
添加标注
BMKAnnotation为标注对应的protocal,您可以自定义标注类实现该protocal。百度地图API也预置了基本的标注点:BMKPointAnnotation,和一个大头针标注View:BMKPinAnnotationView,您可以直接使用来显示标注。示例如下:
修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:
- #import <UIKit/UIKit.h>
- #import "BMapKit.h"
- @interface AnnotationDemoViewController : UIViewController
- <BMKMapViewDelegate>{
- IBOutlet BMKMapView* mapView;
- }
- @end
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForAnnotation:函数,并在viewDidLoad添加标注数据对象
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 设置mapView的Delegate
- mapView.delegate = self;
- // 添加一个PointAnnotation
- BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
- CLLocationCoordinate2D coor;
- coor.latitude = 39.915;
- coor.longitude = 116.404;
- annotation.coordinate = coor;
- annotation.title = @"这里是北京";
- [mapView addAnnotation:annotation];
- }
- // Override
- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
- BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
- newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
- newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
- return newAnnotationView;
- }
- return nil;
- }
运行后,会在地图显示对应的标注点,点击会弹出气泡,效果如图:
删除标注
通过removeAnnotation:函数实现对已添加标注的删除功能,示例如下:
- if (annotation != nil) {
- [mapView removeAnnotation:annotation];
添加折线
修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:
- #import <UIKit/UIKit.h>
- #import "BMapKit.h"
- @interface OverlayDemoViewController : UIViewController <BMKMapViewDelegate>{
- IBOutlet BMKMapView* mapView;
- }
- @end
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加折线数据对象:
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 设置delegate
- mapView.delegate = self;
- // 添加折线覆盖物
- CLLocationCoordinate2D coors[2] = {0};
- coors[0].latitude = 39.315;
- coors[0].longitude = 116.304;
- coors[1].latitude = 39.515;
- coors[1].longitude = 116.504;
- BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
- [mapView addOverlay:polyline];
- }
- // Override
- - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
- if ([overlay isKindOfClass:[BMKPolyline class]]){
- BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease];
- polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
- polylineView.lineWidth = 5.0;
- return polylineView;
- }
- return nil;
- }
运行后,效果如图:
添加多边形
修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加多边形数据对象:
- [super viewDidLoad];
- // 设置delegate
- mapView.delegate = self;
- // 添加多边形覆盖物
- CLLocationCoordinate2D coords[3] = {0};
- coords[0].latitude = 39;
- coords[0].longitude = 116;
- coords[1].latitude = 38;
- coords[1].longitude = 115;
- coords[2].latitude = 38;
- coords[2].longitude = 117;
- BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:3];
- [mapView addOverlay:polygon];
- }
- // Override
- - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
- if ([overlay isKindOfClass:[BMKPolygon class]]){
- BMKPolygonView* polygonView = [[[BMKPolygonView alloc] initWithOverlay:overlay] autorelease];
- polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
- polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
- polygonView.lineWidth = 5.0;
- return polygonView;
- }
- return nil;
- }
运行后,效果如图:
添加圆
修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加园数据对象:
- - (void)viewDidLoad {
- [super viewDidLoad];
- // 设置delegate
- mapView.delegate = self;
- // 添加圆形覆盖物
- CLLocationCoordinate2D coor;
- coor.latitude = 39.915;
- coor.longitude = 116.404;
- BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000];
- [mapView addOverlay:circle];
- }
- // Override
- - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
- if ([overlay isKindOfClass:[BMKCircle class]]){
- BMKCircleView* circleView = [[[BMKCircleView alloc] initWithOverlay:overlay] autorelease];
- circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
- circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
- circleView.lineWidth = 10.0;
- return circleView;
- }
- return nil;
运行后,效果如图:
添加自定义Overlay
从2.0.0开始,地图渲染采用OpenGL方式实现,因此覆盖物基类BMKOverlayView新增glrender接口,以及绘制基本线renderLinesWithPoints、面renderRegionWithPoints的接口来实现对覆盖物的OpenGL渲染。绘制自定义overlay时,继承BMKOverlayView的子类需实现glrender接口,在glrender中通过调用renderLinesWithPoints、renderRegionWithPoints来组合自己想要实现的图形,实例代码如下,详细实例请参见CustomOverlayDemoViewController:
CustomOverlayView继承BMKOverlayPathView,在CustomOverlayView中实现glrender
- - (void)glRender
- {
- //自定义overlay绘制
- CustomOverlay *customOverlay = [self customOverlay];
- if (customOverlay.pointCount >= 3) {
- [self renderRegionWithPoints:customOverlay.points pointCount:customOverlay.pointCount fillColor:self.fillColor usingTriangleFan:YES];//绘制多边形
- }else
- {
- [self renderLinesWithPoints:customOverlay.points pointCount:customOverlay.pointCount strokeColor:self.strokeColor lineWidth:self.lineWidth looped:NO];//绘制线
- }
- }
如果不实现glrender,则需实现drawMapRect默认使用系统GDI绘制,GDI绘制方式在overlayView尺寸较大时可能有效率问题,因此建议使用glrender来实现自定义overlay绘制。
删除Overlay
通过removeOverlay:函数实现对已添加标注的删除功能,示例如下:
- if (overlay != nil) {
- [mapView removeOverlay:overlay];
- }
- 本文转载至:http://developer.baidu.com/map/sdkiosdev-6.htm