Cocoa Touch 没有提供地图注解类,只定义了一个 MKAnnotation 协议。要创建地图注解,必须设计符合 MKAnnotation 协议的类,该类需要一个 CLLocationCoordinate2D coordinate 属性,以及 title 和 subtitle 实例方法,
一. 设计注解类:
例,注解(大头针)类:
.h
#import <MapKit/MapKit.h>
@interface LocationObject : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *_titleString; //title值
NSString *_subTitleString;
float _latitude; // 经度值
float _longitude; //纬度值
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property float _latitude; // 经度值
@property float _longitude; //纬度值
@property (nonatomic, copy) NSString *_titleString; //title值
@property (nonatomic, copy) NSString *_subTitleString;
- (id) initWithTitle:(NSString *)atitle latitue:(float)alatitude longitude:(float)alongitude;
@end
---------------------
.m
@implementation LocationObject
@synthesize coordinate,_latitude,_longitude,_titleString,_subTitleString;
- (id) initWithTitle:(NSString *)atitle latitue:(float)alatitude longitude:(float)alongitude
{
if(self=[super init])
{
self._titleString = atitle;
self._latitude = alatitude;
self._longitude = alongitude;
}
return self;
}
- (CLLocationCoordinate2D)coordinate;
{
CLLocationCoordinate2D currentCoordinate;
currentCoordinate.latitude = self._latitude ;
currentCoordinate.longitude = self._longitude;
return currentCoordinate;
}
// required if you set the MKPinAnnotationView's "canShowCallout" property to YES
- (NSString *)title
{
return self._titleString;
}
// optional
- (NSString *)subtitle
{
return _subTitleString;
}
- (void)dealloc
{
[_titleString release];
[_subTitleString release];
[super dealloc];
}
@end
二、创建、添加和删除注解
1、创建注解:
LocationObject *aLocationObject = [[LocationObject alloc]initWithTitle:nameString latitue:[latitudeString floatValue] longitude:[longitudeString floatValue]];
aLocationObject._subTitleString = addressString;
2、添加注解:
先构建一个注解数组 NSMutableArray *_mapAnnotations;
然后
[self._mapAnnotations addObject:aLocationObject];
[self._mapView addAnnotations:self._mapAnnotations];
3、删除注解:
删除注解可执行 removeAnnotation:一次只删除一个注解,或者执行 removeAnnotation:删除一个数组中的所有项。
如果需要使地图视图回到无注解状态,可执行:
[self._mapView removeAnnotations:self._mapView.annotations];
删除其中全部注解,MKMapView 的 annotations 属性获取了所有注解的数组,然后从地图上全部删除。
三、注解视图
注解对象并非视图,是描述注解的抽象类。注解视图是属于 MKAnnotationView 的子类 MKPinAnnotationView,当地图通过 addAnnotation:或 addAnnotations:添加了注解后,MKMapViewDelegate 协议的委托方法 - (void)mapView:(MKMapView *)mapViewdidAddAnnotationViews:(NSArray *)views 就会通知委托,可以在此回调方法里设置注解视图,如设置大头针颜色、添加附属按钮等,例:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
// Initialize each view
for (MKPinAnnotationView *mkaview in views)
{
// 当前位置 的大头针设为紫色,并且没有右边的附属按钮
if ([mkaview.annotation.title isEqualToString:@"当前位置"])
{
mkaview.pinColor = MKPinAnnotationColorPurple;
mkaview.rightCalloutAccessoryView = nil;
continue;
}
// 其他位置的大头针设为红色,右边添加附属按钮
mkaview.pinColor = MKPinAnnotationColorRed;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
mkaview.rightCalloutAccessoryView = button;
}
}
四、注解视图 MKPinAnnotationView 的几个属性说明
newAnnotation.animatesDrop = YES; // 大头针掉落的动画开启,NO-关闭
newAnnotation.canShowCallout=YES; // 控制轻击按钮是否生成一个注解视图,默认为Yes-开启
newAnnotation.pinColor // 设置大头针颜色,一共有三种颜色:红色(MKPinAnnotationColorRed),绿色(MKPinAnnotationColorGreen),紫色(MKPinAnnotationColorPurple)
五、自动显示注解视图(Callout)
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
。。。。。。
///// 自动显示 Callout
_myAnnotation = annotation;
[self performSelector:@selector(showCallout) withObject:selfafterDelay:0.1];
return newAnnotation;
}
- (void)showCallout {
[self._mapView selectAnnotation:_myAnnotation animated:YES];
}
CGRect rect = CGRectMake(0, 20, 320, 460); MKMapView *mapView = [[MKMapView alloc] initWithFrame:rect];
CLLocationCoordinate2D theCoordinate; theCoordinate.latitude=24.148926; theCoordinate.longitude=120.715542;
MKCoordinateSpan theSpan; theSpan.latitudeDelta=0.1; theSpan.longitudeDelta=0.1;
MKCoordinateRegion theRegion; theRegion.center=theCoordinate; theRegion.span=theSpan;
[mapView setMapType:MKMapTypeStandard]; [mapView setRegion:theRegion];
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier