iOS Annotation

1. 添加到map view的子视图不会随地图的移动而移动,map view会固定其子视图的位置。如果要添加随着地图移动的子视图,可以使用annotations和overlays。annotation用来显示由一个经纬度定义的位置,而overlay则是由多个点所定义或者包含了许多连续的图形。
 
2.在地图上显示annotation,需要提供两个对象
annotation object)
annotation view.)
注释对象通常是一些小的数据对象,保存了地图的坐标和一些相关信息。
Map Kit提供了一些标准的注释视图,你也可以使用自定义的注释视图。但是不能将注释视图直接添加到map view,而是使用map view的代理对象来提供。
 
3.添加注释的具体步骤
定义一个注释对象annotation object :
使用MKPointAnnotation类来实现一个简单的注释,这类注释可以显示标题和副标题。
自定义一个遵守MKAnnotation协议的对象,这类注释可以存储任何类型数据
定义一个注释视图annotation view来显示数据:
如果注释可以由一张静态图片表示,则创建一个MKAnnotationView类的实例,然后将图像赋值给image属性
如果你想使用标准的pin annotation,创建一个MKPinAnnotationView类的实例
如果静态图像不够表示你的注释,那么创建一个MKAnnotationView的子类
Implement the mapView:viewForAnnotation: method in your map view delegate. Your implementation of this method should dequeue an existing annotation view if one exists or create a new one. If your application supports multiple types of annotations, you must include logic in this method to create a view of the appropriate type for the provided annotation object. 在map view的代理对象中实现mapView:viewForAnnotation:方法,使用已经存在的注释视图或者新创建一个。如果你的程序支持多种不同的注释,那么应该根据不同的注释提供不同的视图
方法添加你的注释对象到map view
 
4.自定义注释对象
注释对象遵守MKAnnotation协议,如果你之想要将一个标题和一个坐标相联系,那么可以直接使用MKPointAnnotation作为注释对象。如果还想要显示其他信息,就需要自定义一个注释对象
 
Creating a simple annotation object   


[cpp]  view plain copy
  1. @interface MyCustomAnnotation : NSObject <MKAnnotation> {     
  2.     CLLocationCoordinate2D coordinate;     
  3. }     
  4. @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;     
  5. - (id)initWithLocation:(CLLocationCoordinate2D)coord;     
  6.       
  7. // Other methods and properties.     
  8. @end    
  9.    
  10.    
  11. @implementation MyCustomAnnotation     
  12. @synthesize coordinate;     
  13.       
  14. - (id)initWithLocation:(CLLocationCoordinate2D)coord {     
  15.     self = [super init];     
  16.     if (self) {     
  17.         coordinate = coord;     
  18.     }     
  19.     return self;     
  20. }     
  21. @end    
 
5.使用标准注释视图
MKAnnotationView类定义了注释视图的一些基本特性。MKPinAnnotationView类是MKAnnotationView的子类,用来显示系统标准的注释视图(pin view)
创建一个MKAnnotationView的实例,设置image属性。当此annotation显示在地图上时,该图像显示在相应的坐标位置(If you do not want the image to be centered on the map coordinate, you can use the centerOffset property to move the center point horizontally and vertically in any direction. )。
 
[cpp]  view plain copy
  1. MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation      
  2.                                   reuseIdentifier:@"MyCustomAnnotation"] autorelease];     
  3. aView.image = [UIImage imageNamed:@"myimage.png"];     
  4. aView.centerOffset = CGPointMake(10, -20);     


在代理的mapView:viewForAnnotation:方法中创建标准注释视图
 
6.使用自定义注释视图
 
[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>     
  2. #import <MapKit/MapKit.h>     
  3.       
  4. @interface MyCustomAnnotationView : MKAnnotationView     
  5. {     
  6.    // Custom data members     
  7. }     
  8. // Custom properties and methods.     
  9. @end    
  10.    
  11.    
  12.  Initializing a custom annotation view     
  13. - (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier     
  14. {     
  15.     self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];     
  16.     if (self)     
  17.     {     
  18.         // Set the frame size to the appropriate values.     
  19.         CGRect  myFrame = self.frame;     
  20.         myFrame.size.width = 40;     
  21.         myFrame.size.height = 40;     
  22.         self.frame = myFrame;     
  23.       
  24.         // The opaque property is YES by default. Setting it to     
  25.         // NO allows map content to show through any unrendered     
  26.         // parts of your view.     
  27.         self.opaque = NO;     
  28.     }     
  29.     return self;     
  30. }    

 
6.在代理对象中创建注释视图
 
 Creating annotation views   
[cpp]  view plain copy
  1. - (MKAnnotationView *)mapView:(MKMapView *)mapView     
  2.                       viewForAnnotation:(id <MKAnnotation>)annotation     
  3. {     
  4.     // If it's the user location, just return nil.     
  5.     if ([annotation isKindOfClass:[MKUserLocation class]])     
  6.         return nil;     
  7.       
  8.     // Handle any custom annotations.     
  9.     if ([annotation isKindOfClass:[MyCustomAnnotation class]])     
  10.     {     
  11.         // Try to dequeue an existing pin view first.     
  12.         MKPinAnnotationView*    pinView = (MKPinAnnotationView*)[mapView     
  13.         dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];     
  14.       
  15.         if (!pinView)     
  16.         {     
  17.             // If an existing pin view was not available, create one.     
  18.            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation      
  19.                        reuseIdentifier:@"CustomPinAnnotation"]     
  20.                              autorelease];     
  21.             pinView.pinColor = MKPinAnnotationColorRed;     
  22.             pinView.animatesDrop = YES;     
  23.             pinView.canShowCallout = YES;     
  24.       
  25.             // Add a detail disclosure button to the callout.     
  26.             UIButton* rightButton = [UIButton buttonWithType:     
  27.                                UIButtonTypeDetailDisclosure];     
  28.             [rightButton addTarget:self action:@selector(myShowDetailsMethod:)     
  29.                                forControlEvents:UIControlEventTouchUpInside];     
  30.             pinView.rightCalloutAccessoryView = rightButton;     
  31.         }     
  32.         else     
  33.             pinView.annotation = annotation;     
  34.       
  35.         return pinView;     
  36.     }     
  37.      
  38.     return nil;     
  39. }   
  40.    


当map view需要显示注释时候,会调用代理的mapView:viewForAnnotation:方法,如果你不实现此方法或者总是返回nil,map view会使用默认的注释视图,即pin annotation view。在你创建新的视图之前,检查是否已经存在此类视图dequeueReusableAnnotationViewWithIdentifier: ,类似于tableview的cell实现
 
7.管理地图的注释对象
为了避免注释视图的重叠,在方法mapView:regionWillChangeAnimated: andmapView:regionDidChangeAnimated: 中,根据需要来添加或者减少
 
8.让注释视图可拖动
在注释对象中,实现setCoordinate:方法来更新注释的坐标。
创建注释视图时,设置draggable属性为YES
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值