前言


ios上边使用地图库的同学肯定遇到过这样的问题:吹出框只能设置title和subtitle和左右的view,不管是百度地图还是高德地图还是自带的
google地图,只提供了这四个属性,如果想添加更多的view,只能自定义。可是,类库只能看到.h文件,.m都看不到,这让新手比较蛋疼,庞大的地
图类库一时半会摸不着头脑,从头再学还需要时间,本文就教大家快速制作一个属于自己的
CalloutView!等你一步一步调通后,再回过头来使用系统自带的方法设置callout,就会领悟这个过程。

正文

Xcode版本:4.6.1

SDK版本:6.0

百度地图版本:1.2.2(关于地图不必纠结,无论是百度还是高德还是google都是基于系统的MapKit,都是一样的)

demo模式:非ARC,使用storyboard。

demo资源:

http://download.csdn.net/detail/mad1989/5252037

Step1

创建demo,并添加百度地图的静态类库,helloword能显示mapview

关于这一步我专门写了教程,这里就不再赘述,同样,关于如何使用自带的BMKPointAnnotation添加一个marker,我也不再说了,如果连这个你都不会,那么先去官网看一下基本教程。

Step2

实现三个委托方法:

这个方法类似tableview添加cell,都是创建annotation

  1. #pragma mark
  2. #pragma mark - BMKMapview delegate
  3. -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation;

这个方法在点击地图marker时所触发(并显示callout)

  1. -(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view;

这个方法在点击地图任意位置,相当于隐藏callout

  1. -(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view;

原理:地图上的marker是在viewForAnnoation里创建的,同时也会
隐含的为我们创建一个CalloutView,就是自带的吹出框,只是我们看不到源码。其实这个吹出框(CalloutView)也是一个
annotation,也会在viewForAnnotation里被创建,他的坐标应该和这个点的marker坐标一样,只要明白了这一点,就行了,marker和吹出框是两个不同的annotation,他们有同样的coordinate。

Step3

自定义一个Annotation,为了简单方便,我就直接继承了mapview自带的BMKPointAnnotation,这是一个经典的图钉marker。

在这里我添加了一个Dictionary属性,目的是为了自定义的CalloutView吹出框显示内容赋值,一会就明白了。

Step4

添加自定义Annotation到mapview

  1. //添加自定义Annotation
  2. CLLocationCoordinate2D center = {39.91669,116.39716};
  3. CustomPointAnnotation *pointAnnotation = [[CustomPointAnnotation alloc] init];
  4. pointAnnotation.title = @"我是中国人";//因为继承了BMKPointAnnotation,所以这些title,subtitle都可以设置
  5. pointAnnotation.subtitle = @"我爱自己的祖国";
  6. pointAnnotation.coordinate = center;
  7. [mymapview addAnnotation:pointAnnotation];
  8. [pointAnnotation release];

在viewForanntion里,由于我对marker没太大要求,直接使用了BMKPinAnnotationView(图钉),简单设置image属性为自己需要的图标,如下所示:

展示一个效果图:

显然CalloutView只能设置title和subtitle,无法满足我们的要求,那么继续下一步。

Step5

创建一个(自定义的CalloutView)的Annotation,相当于显示calloutView的annotation。

[注意] 继承自NSObject<BMKAnnotation>

CalloutMapAnnotation.h

  1. #import <Foundation/Foundation.h>
  2. #import "BMapKit.h"
  3. @interface CalloutMapAnnotation : NSObject<BMKAnnotation>
  4. @property (nonatomic) CLLocationDegrees latitude;
  5. @property (nonatomic) CLLocationDegrees longitude;
  6. @property(retain,nonatomic) NSDictionary *locationInfo;//callout吹出框要显示的各信息
  7. - (id)initWithLatitude:(CLLocationDegrees)lat andLongitude:(CLLocationDegrees)lon;
  8. @end

CalloutMapAnnotation.m

  1. #import "CalloutMapAnnotation.h"
  2. @implementation CalloutMapAnnotation
  3. @synthesize latitude;
  4. @synthesize longitude;
  5. @synthesize locationInfo;
  6. - (id)initWithLatitude:(CLLocationDegrees)lat
  7. andLongitude:(CLLocationDegrees)lon {
  8. if (self = [super init]) {
  9. self.latitude = lat;
  10. self.longitude = lon;
  11. }
  12. return self;
  13. }
  14. -(CLLocationCoordinate2D)coordinate{
  15. CLLocationCoordinate2D coordinate;
  16. coordinate.latitude = self.latitude;
  17. coordinate.longitude = self.longitude;
  18. return coordinate;
  19. }
  20. @end

这里设置了经纬度的属性,和一个init初始化经纬度的方法(经纬度=marker的经纬度),同样添加了一个Dictionary的属性,为了传递在CalloutView上内容的赋值,继续。

Step6

这一步我们创建自定义的View,想要什么布局就写什么样的布局,想要多少属性就加多少属性,这里我使用了code方式画了一个contentView,里面的子view使用Xib方式创建。

[注意:继承自BMKAnnotationView]

CallOutAnnotationView.h

  1. #import "BMKAnnotationView.h"
  2. #import "BusPointCell.h"
  3. @interface CallOutAnnotationView : BMKAnnotationView
  4. @property(nonatomic,retain) UIView *contentView;
  5. //添加一个UIView
  6. @property(nonatomic,retain) BusPointCell *busInfoView;//在创建calloutView Annotation时,把contentView add的 subview赋值给businfoView
  7. @end

BusPointCell是ContentView里的subview,这个view就是显示各个组件,并赋不同的值

CallOutAnnotationView.m

  1. #import "CallOutAnnotationView.h"
  2. #import <QuartzCore/QuartzCore.h>
  3. #define  Arror_height 6
  4. @implementation CallOutAnnotationView
  5. @synthesize contentView;
  6. @synthesize busInfoView;
  7. - (id)initWithFrame:(CGRect)frame
  8. {
  9. self = [super initWithFrame:frame];
  10. if (self) {
  11. }
  12. return self;
  13. }
  14. -(void)dealloc{
  15. [contentView release];
  16. [busInfoView release];
  17. [super dealloc];
  18. }
  19. -(id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
  20. self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  21. if (self) {
  22. self.backgroundColor = [UIColor clearColor];
  23. self.canShowCallout = NO;
  24. self.centerOffset = CGPointMake(0, -55);
  25. self.frame = CGRectMake(0, 0, 240, 80);
  26. UIView *_contentView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.frame.size.width-15, self.frame.size.height-15)];
  27. _contentView.backgroundColor   = [UIColor clearColor];
  28. [self addSubview:_contentView];
  29. self.contentView = _contentView;
  30. [_contentView release];
  31. }
  32. return self;
  33. }
  34. -(void)drawRect:(CGRect)rect{
  35. [self drawInContext:UIGraphicsGetCurrentContext()];
  36. self.layer.shadowColor = [[UIColor blackColor] CGColor];
  37. self.layer.shadowOpacity = 1.0;
  38. self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
  39. }
  40. -(void)drawInContext:(CGContextRef)context
  41. {
  42. CGContextSetLineWidth(context, 2.0);
  43. CGContextSetFillColorWithColor(context, [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0].CGColor);
  44. [self getDrawPath:context];
  45. CGContextFillPath(context);
  46. }
  47. - (void)getDrawPath:(CGContextRef)context
  48. {
  49. CGRect rrect = self.bounds;
  50. CGFloat radius = 6.0;
  51. CGFloat minx = CGRectGetMinX(rrect),
  52. midx = CGRectGetMidX(rrect),
  53. maxx = CGRectGetMaxX(rrect);
  54. CGFloat miny = CGRectGetMinY(rrect),
  55. // midy = CGRectGetMidY(rrect),
  56. maxy = CGRectGetMaxY(rrect)-Arror_height;
  57. CGContextMoveToPoint(context, midx+Arror_height, maxy);
  58. CGContextAddLineToPoint(context,midx, maxy+Arror_height);
  59. CGContextAddLineToPoint(context,midx-Arror_height, maxy);
  60. CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
  61. CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
  62. CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
  63. CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
  64. CGContextClosePath(context);
  65. }
  66. @end

BusPointCell.h

想要多少label,就可以有多少label

  1. #import <UIKit/UIKit.h>
  2. @interface BusPointCell : UIView
  3. @property (retain, nonatomic) IBOutlet UILabel *aliasLabel;
  4. @property (retain, nonatomic) IBOutlet UILabel *speedLabel;
  5. @property (retain, nonatomic) IBOutlet UILabel *degreeLabel;
  6. @property (retain, nonatomic) IBOutlet UILabel *nameLabel;
  7. @end

BusPointCell.m

  1. #import "BusPointCell.h"
  2. @implementation BusPointCell
  3. - (id)initWithFrame:(CGRect)frame
  4. {
  5. self = [super initWithFrame:frame];
  6. if (self) {
  7. }
  8. return self;
  9. }
  10. - (void)dealloc {
  11. [_aliasLabel release];
  12. [_speedLabel release];
  13. [_degreeLabel release];
  14. [_nameLabel release];
  15. [super dealloc];
  16. }
  17. @end

BusPointCell.xib

Step7

自定义的CalloutView都准备妥当,现在就是要实现他们的部分了,简单说一下原理,在didSelectAnnotationView函数里创建并添加calloutview的annotation(CalloutMapAnnotation),然后在viewForAnnotation函数内实例化要显示的calloutview(CallOutAnnotationView)

首先声明一个局部变量CalloutMapAnnotation *_calloutMapAnnotation;

在didSelectAnnotationView函数内添加如下代码:

  1. //CustomPointAnnotation 是自定义的marker标注点,通过这个来得到添加marker时设置的pointCalloutInfo属性
  2. CustomPointAnnotation *annn = (CustomPointAnnotation*)view.annotation;
  3. if ([view.annotation isKindOfClass:[CustomPointAnnotation class]]) {
  4. //如果点到了这个marker点,什么也不做
  5. if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
  6. _calloutMapAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
  7. return;
  8. }
  9. //如果当前显示着calloutview,又触发了select方法,删除这个calloutview annotation
  10. if (_calloutMapAnnotation) {
  11. [mapView removeAnnotation:_calloutMapAnnotation];
  12. _calloutMapAnnotation=nil;
  13. }
  14. //创建搭载自定义calloutview的annotation
  15. _calloutMapAnnotation = [[[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude] autorelease];
  16. //把通过marker(ZNBCPointAnnotation)设置的pointCalloutInfo信息赋值给CalloutMapAnnotation
  17. _calloutMapAnnotation.locationInfo = annn.pointCalloutInfo;
  18. [mapView addAnnotation:_calloutMapAnnotation];
  19. [mapView setCenterCoordinate:view.annotation.coordinate animated:YES];
  20. }

其次,要在viewForAnnotation里创建我们的calloutview(CallOutAnnotationView),添加如下代码:

  1. else if ([annotation isKindOfClass:[CalloutMapAnnotation class]]){
  2. //此时annotation就是我们calloutview的annotation
  3. CalloutMapAnnotation *ann = (CalloutMapAnnotation*)annotation;
  4. //如果可以重用
  5. CallOutAnnotationView *calloutannotationview = (CallOutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"calloutview"];
  6. //否则创建新的calloutView
  7. if (!calloutannotationview) {
  8. calloutannotationview = [[[CallOutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"calloutview"] autorelease];
  9. BusPointCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"BusPointCell" owner:self options:nil] objectAtIndex:0];
  10. [calloutannotationview.contentView addSubview:cell];
  11. calloutannotationview.busInfoView = cell;
  12. }
  13. //开始设置添加marker时的赋值
  14. calloutannotationview.busInfoView.aliasLabel.text = [ann.locationInfo objectForKey:@"alias"];
  15. calloutannotationview.busInfoView.speedLabel.text = [ann.locationInfo objectForKey:@"speed"];
  16. calloutannotationview.busInfoView.degreeLabel.text =[ann.locationInfo objectForKey:@"degree"];
  17. calloutannotationview.busInfoView.nameLabel.text =  [ann.locationInfo objectForKey:@"name"];
  18. return calloutannotationview;
  19. }

[注意]在添加marker的判断里一定要设置markerannotation.canShowCallout =NO;
否则点击marker会默认显示系统的吹出框

Step8

calloutview的annotation也创建和添加了,接下来我们就设置一下marker对应吹出框的数据:

然后运行一下:

哈哈!搞定了吧,具体布局可以自己通过code方式,或xib方式设计,目前点击marker能显示了,可是点击其它区域还是无法显示,所以我们在didDeselectAnnotationView方法里还需要判断一下,remove掉。

  1. -(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{
  2. if (_calloutMapAnnotation&&![view isKindOfClass:[CallOutAnnotationView class]]) {
  3. if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
  4. _calloutMapAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
  5. [mapView removeAnnotation:_calloutMapAnnotation];
  6. _calloutMapAnnotation = nil;
  7. }
  8. }
  9. }

最后

之所以在显示marker的annotation[本文为CustomPointAnnotation]和显示calloutview的annotation[本文为CalloutMapAnnotation]里各添加一个Dictionary,就是要在点击时通过marker传递数据,添加时通过calloutview的annotation实例来设置每一个属性的数据,已达到不同的maker,显示不同的数据。

可能我的过程不是太清晰,自己仔细研究一下这三个函数和mapview自带的callout调用过程,便会明白。

dmeo地址:http://download.csdn.net/detail/mad1989/5252037

最新文章

  1. java继承覆盖与向上转型,权限
  2. IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
  3. Fragment与FragmentAcitvity间的传值
  4. 每日学习心得:$.extend()方法和(function($){...})(jQuery)详解
  5. SQL Server 2008 删除数据库帐号失败问题
  6. 未能加载文件或程序集“SQLDAL”或它的某一个依赖项。系统找不到指定的文件
  7. PAT 05-树6 Path in a Heap
  8. IOS 真机调试以及发布应用 1
  9. javaweb 国际化
  10. .net 各种序列化方式效率对比
  11. springboot高并发redis细粒度加锁(key粒度加锁)
  12. 最小化安装linux CentOS_7操作系统
  13. HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
  14. 洛谷 [P1129] [ZJOI2007] 矩阵游戏
  15. Nodejs一键实现微信内打开网页url自动跳转外部浏览器访问的功能
  16. WinForm中一个窗体调用另一个窗体
  17. 前端之html表单
  18. Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)
  19. java.lang.OutOfMemoryError: unable to create new native thread 居然是MQ问题
  20. 内置函数bytes()

热门文章

  1. Hadoop 发行版本 Hortonworks 安装详解(一) 准备工作
  2. IndentationError: unexpected indent
  3. Android WebView存在跨域访问漏洞(CNVD-2017-36682)介绍及解决
  4. android自定义文本框,后面带清空按钮
  5. eml企业通讯录管理系统v5.0 存在sql注入
  6. windows系统操作
  7. C#高级编程学习一-----------------第五章泛型
  8. 无法向Windows服务器复制粘贴文件
  9. 【Win 10 应用开发】MIDI 音乐合成——更改乐器音色
  10. gulp基于node流的自动化构建工具