(ios开发)在MapKit地图上添加注释

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作为注释对象。如果还想要显示其他信息,就需要自定义一个注释对象
 
<span style="font-size:16px;">Listing 5-1  Creating a simple annotation object   
@@interface MyCustomAnnotation : NSObject <MKAnnotation> {   
    CLLocationCoordinate2D coordinate;   
}   
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;   
- (id)initWithLocation:(CLLocationCoordinate2D)coord;   
    
// Other methods and properties.   
@end  </span> 
 
 
<span style="font-size:16px;">@implementation MyCustomAnnotation   
@synthesize coordinate;   
    
- (id)initWithLocation:(CLLocationCoordinate2D)coord {   
    self = [super init];   
    if (self) {   
        coordinate = coord;   
    }   
    return self;   
}   
@end  </span> 
 
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. )。
 
<span style="font-size:16px;">MKAnnotationView* aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation    
                                  reuseIdentifier:@"MyCustomAnnotation"] autorelease];   
aView.image = [UIImage imageNamed:@"myimage.png"];   
aView.centerOffset = CGPointMake(10, -20);  </span> 
在代理的mapView:viewForAnnotation:方法中创建标准注释视图
 
6.使用自定义注释视图
 
<span style="font-size:16px;">#import <UIKit/UIKit.h>   
#import <MapKit/MapKit.h>   
    
@interface MyCustomAnnotationView : MKAnnotationView   
{   
   // Custom data members   
}   
// Custom properties and methods.   
@end  </span> 
 
 
<span style="font-size:16px;">Listing 5-5  Initializing a custom annotation view   
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier   
{   
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];   
    if (self)   
    {   
        // Set the frame size to the appropriate values.   
        CGRect  myFrame = self.frame;   
        myFrame.size.width = 40;   
        myFrame.size.height = 40;   
        self.frame = myFrame;   
    
        // The opaque property is YES by default. Setting it to   
        // NO allows map content to show through any unrendered   
        // parts of your view.   
        self.opaque = NO;   
    }   
    return self;   
}  </span> 
 
 
6.在代理对象中创建注释视图 www.2cto.com
 
 
<span style="font-family:Arial;color:#333333;"><span style="font-size:16px;">Listing 5-6  Creating annotation views   
- (MKAnnotationView *)mapView:(MKMapView *)mapView   
                      viewForAnnotation:(id <MKAnnotation>)annotation   
{   
    // If it's the user location, just return nil.   
    if ([annotation isKindOfClass:[MKUserLocation class]])   
        return nil;   
    
    // Handle any custom annotations.   
    if ([annotation isKindOfClass:[MyCustomAnnotation class]])   
    {   
        // Try to dequeue an existing pin view first.   
        MKPinAnnotationView*    pinView = (MKPinAnnotationView*)[mapView   
        dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];   
    
        if (!pinView)   
        {   
            // If an existing pin view was not available, create one.   
           pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation    
                       reuseIdentifier:@"CustomPinAnnotation"]   
                             autorelease];   
            pinView.pinColor = MKPinAnnotationColorRed;   
            pinView.animatesDrop = YES;   
            pinView.canShowCallout = YES;   
    
            // Add a detail disclosure button to the callout.   
            UIButton* rightButton = [UIButton buttonWithType:   
                               UIButtonTypeDetailDisclosure];   
            [rightButton addTarget:self action:@selector(myShowDetailsMethod:)   
                               forControlEvents:UIControlEventTouchUpInside];   
            pinView.rightCalloutAccessoryView = rightButton;   
        }   
        else   
            pinView.annotation = annotation;   
    
        return pinView;   
    }   
   
    return nil;   
}  </span></span> 
 
当map view需要显示注释时候,会调用代理的mapView:viewForAnnotation:方法,如果你不实现此方法或者总是返回nil,map view会使用默认的注释视图,即pin annotation view。在你创建新的视图之前,检查是否已经存在此类视图dequeueReusableAnnotationViewWithIdentifier: ,类似于tableview的cell实现
 
7.管理地图的注释对象
为了避免注释视图的重叠,在方法mapView:regionWillChangeAnimated: andmapView:regionDidChangeAnimated: 中,根据需要来添加或者减少
 
8.让注释视图可拖动
在注释对象中,实现setCoordinate:方法来更新注释的坐标。
创建注释视图时,设置draggable属性为YES


posted @ 2012-07-11 17:41  hhhker  阅读(3682)  评论(0编辑  收藏  举报