热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

地图覆盖物

覆盖物概述地图上自定义的标注点和覆盖物我们统称为地图覆盖物。您可以通过定制BMKAnnotation和BMKOverlay来添加对应的标注点和覆盖物。地图覆盖物的设计遵循数据与Vi

覆盖物概述

地图上自定义的标注点和覆盖物我们统称为地图覆盖物。您可以通过定制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协议:

源码关于



  1. #import   

  2. #import "BMapKit.h"  

  3. @interface AnnotationDemoViewController : UIViewController   

  4. {  

  5.     IBOutlet BMKMapView* mapView;  

  6. }  

  7. @end  

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

源码关于



  1. // Implement viewDidLoad to do additional setup after loading       the view, typically from a nib.  

  2. - (void)viewDidLoad {  

  3.     [super viewDidLoad];  

  4.     // 设置mapView的Delegate  

  5.     mapView.delegate = self;  

  6.     // 添加一个PointAnnotation  

  7.     BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];  

  8.     CLLocationCoordinate2D coor;  

  9.     coor.latitude = 39.915;  

  10.     coor.longitude = 116.404;  

  11.     annotation.coordinate = coor;  

  12.     annotation.title = @"这里是北京";  

  13.     [mapView addAnnotation:annotation];  

  14. }  

  15. // Override  

  16. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id )annotation  

  17. {  

  18.     if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {  

  19.         BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];     

  20.         newAnnotationView.pinColor = BMKPinAnnotationColorPurple;     

  21.         newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示  

  22.         return newAnnotationView;     

  23.     }  

  24.     return nil;  

  25. }  

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


删除标注

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

源码关于



  1. if (annotation != nil) {  

  2. [mapView removeAnnotation:annotation];  


添加折线

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

源码关于



  1. #import   

  2. #import "BMapKit.h"  

  3. @interface OverlayDemoViewController : UIViewController {  

  4.     IBOutlet BMKMapView* mapView;  

  5. }  

  6. @end  

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

源码关于



  1. - (void)viewDidLoad {  

  2.     [super viewDidLoad];  

  3.     // 设置delegate  

  4.     mapView.delegate = self;  

  5.     // 添加折线覆盖物  

  6.     CLLocationCoordinate2D coors[2] = {0};  

  7.     coors[0].latitude = 39.315;  

  8.     coors[0].longitude = 116.304;  

  9.     coors[1].latitude = 39.515;  

  10.     coors[1].longitude = 116.504;  

  11.     BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];  

  12.     [mapView addOverlay:polyline];  

  13. }  

  14. // Override  

  15. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id )overlay{  

  16.     if ([overlay isKindOfClass:[BMKPolyline class]]){  

  17.         BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease];  

  18.         polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];  

  19.         polylineView.lineWidth = 5.0;  

  20.         return polylineView;  

  21.     }  

  22.     return nil;  

  23. }  

运行后,效果如图:


添加多边形

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

源码关于



  1. [super viewDidLoad];   

  2. // 设置delegate  

  3.     mapView.delegate = self;  

  4.     // 添加多边形覆盖物  

  5.     CLLocationCoordinate2D coords[3] = {0};  

  6.     coords[0].latitude = 39;  

  7.     coords[0].longitude = 116;  

  8.     coords[1].latitude = 38;  

  9.     coords[1].longitude = 115;  

  10.     coords[2].latitude = 38;  

  11.     coords[2].longitude = 117;  

  12.     BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:3];  

  13.     [mapView addOverlay:polygon];  

  14. }  

  15. // Override  

  16. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id )overlay{      

  17.     if ([overlay isKindOfClass:[BMKPolygon class]]){  

  18.         BMKPolygonView* polygonView = [[[BMKPolygonView alloc] initWithOverlay:overlay] autorelease];  

  19.         polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];  

  20.     polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];  

  21.         polygonView.lineWidth = 5.0;  

  22.         return polygonView;  

  23.     }  

  24.     return nil;  

  25. }  

运行后,效果如图:


添加圆

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

源码关于



  1. - (void)viewDidLoad {  

  2.     [super viewDidLoad];  

  3.     // 设置delegate  

  4.     mapView.delegate = self;  

  5.     // 添加圆形覆盖物  

  6.     CLLocationCoordinate2D coor;  

  7.     coor.latitude = 39.915;  

  8.     coor.longitude = 116.404;  

  9.     BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000];  

  10.     [mapView addOverlay:circle];  

  11. }  

  12. // Override  

  13. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id )overlay{  

  14.     if ([overlay isKindOfClass:[BMKCircle class]]){  

  15.         BMKCircleView* circleView = [[[BMKCircleView alloc] initWithOverlay:overlay] autorelease];  

  16.         circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];  

  17.         circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];  

  18.         circleView.lineWidth = 10.0;  

  19.         return circleView;  

  20.       }  

  21.      return nil;  

运行后,效果如图:


添加自定义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.     {  

  3.     //自定义overlay绘制  

  4.     CustomOverlay *customOverlay = [self customOverlay];  

  5.   

  6.     if (customOverlay.pointCount >= 3) {  

  7.         [self renderRegionWithPoints:customOverlay.points pointCount:customOverlay.pointCount fillColor:self.fillColor usingTriangleFan:YES];//绘制多边形  

  8.     }else  

  9.     {  

  10.         [self renderLinesWithPoints:customOverlay.points pointCount:customOverlay.pointCount strokeColor:self.strokeColor lineWidth:self.lineWidth looped:NO];//绘制线  

  11.     }  

  12. }  

  13.    

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


删除Overlay

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

源码关于




    1. if (overlay != nil) {  

    2.     [mapView removeOverlay:overlay];  

    3. }  

    4.     本文转载至:http://developer.baidu.com/map/sdkiosdev-6.htm




推荐阅读
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 如何在窗口右下角添加调整大小的手柄
    本文探讨了如何在传统MFC/Win32 API编程中实现类似C# WinForms中的SizeGrip功能,即在窗口的右下角显示一个用于调整窗口大小的手柄。我们将介绍具体的实现方法和相关API。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文详细介绍了Java中的访问器(getter)和修改器(setter),探讨了它们在保护数据完整性、增强代码可维护性方面的重要作用。通过具体示例,展示了如何正确使用这些方法来控制类属性的访问和更新。 ... [详细]
  • 使用Python在SAE上开发新浪微博应用的初步探索
    最近重新审视了新浪云平台(SAE)提供的服务,发现其已支持Python开发。本文将详细介绍如何利用Django框架构建一个简单的新浪微博应用,并分享开发过程中的关键步骤。 ... [详细]
  • 本文详细介绍了如何准备和安装 Eclipse 开发环境及其相关插件,包括 JDK、Tomcat、Struts 等组件的安装步骤及配置方法。 ... [详细]
  • 堆是一种常见的数据结构,广泛应用于计算机科学领域。它通常表示为一棵完全二叉树,并可通过数组实现。堆的主要特性是每个节点的值与其父节点的值之间存在特定的关系,这使得堆在优先队列和排序算法中非常有用。 ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
author-avatar
yvli心语
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有