MKMapView使用的一些技巧

项目里要用到一些地图的交互功能,于是研究了下这个东西。

最基本是就是在mapview上加大头针标注了,需要自己写一个类,实现mkannotation的protocol.

下面是一个例子:

#import <MapKit/MapKit.h>

@interface SSMapAnnotation : NSObject <MKAnnotation> {

@private
	
	CLLocationCoordinate2D _coordinate;
	NSString * _title;
	NSString * _subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

+ (SSMapAnnotation *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)aCoordinate;
+ (SSMapAnnotation *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle;
+ (SSMapAnnotation *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle subtitle:(NSString *)aSubtitle;

- (SSMapAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate;
- (SSMapAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle;
- (SSMapAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle subtitle:(NSString *)aSubtitle;

@end

 
#import "SSMapAnnotation.h"

@implementation SSMapAnnotation

@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;

#pragma mark -
#pragma mark Class Methods

+ (SSMapAnnotation *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)aCoordinate {
	return [self mapAnnotationWithCoordinate:aCoordinate title:nil subtitle:nil];
}


+ (SSMapAnnotation *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle {
	return [self mapAnnotationWithCoordinate:aCoordinate title:aTitle subtitle:nil];
}


+ (SSMapAnnotation *)mapAnnotationWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle subtitle:(NSString *)aSubtitle {
	SSMapAnnotation *annotation = [[[self alloc] init] autorelease];
	annotation.coordinate = aCoordinate;
	annotation.title = aTitle;
	annotation.subtitle = aSubtitle;
	return annotation;
}


#pragma mark -
#pragma mark NSObject

- (void)dealloc {
	[_title release];
	[_subtitle release];
	[super dealloc];
}


#pragma mark -
#pragma mark Initializers

- (SSMapAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate {
	return [self initWithCoordinate:aCoordinate title:nil subtitle:nil];
}


- (SSMapAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle {
	return [self initWithCoordinate:aCoordinate title:aTitle subtitle:nil];
}


- (SSMapAnnotation *)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate title:(NSString *)aTitle subtitle:(NSString *)aSubtitle {
	if (self = [super init]) {
		self.coordinate = aCoordinate;
		self.title = aTitle;
		self.subtitle = aSubtitle;
	}
	return self;
}

@end

然后创建SSMapAnnotation的实例:SSMapAnnotation * myAnnotation = [[SSMapAnnotation alloc]iniWithCoordinate:theCoordinate title:@"xxx" subtitle:@""];

加入mapview: [mapView addAnnotation:myAnnotation] 就成功了。

有时候mapview上可能有很多annotations,但想要它们都显示在一个screen里,怎么办呢?可以用下面的方法:

1.计算出这些annotations的maxLatitude/minLatitude和maxLongitude/minLongitude

2.用上面计算好的参数,配置一个MKCoordinateRegion

3.设置mapView的region为配置好的region

相关代码如下:

            
            MKCoordinateRegion extents;
            extents.center.latitude = (maxLatitude + minLatitude)/2.0;
            extents.center.longitude = (maxLongitude + minLongitude)/2.0;
            extents.span.latitudeDelta = (maxLatitude - minLatitude);
            extents.span.longitudeDelta = (maxLongitude - minLongitude);
            
            MKCoordinateRegion fittedRegion = [mapView regionThatFits:extents];
            [mapView setRegion:fittedRegion animated:YES];

这样你所有的annotations都在一个screen里显示出来了。

未完待续。。


你可能感兴趣的:(Class,interface,Annotations,methods)