您的位置:首页 > 其它

MKAnnotationView,MKAnnotation与MKMapView结合使用显示坐标点

2011-12-15 10:19 393 查看
☉限制:必须将iPhone的操作系统更新到ios3.0版本,开发使用的SDK也要是SDK 3[b].0[/b]才有內建Mapkit
Framework


☉效果画面:





☉步骤说明:

在 地图上每一个记号,都是一個MKAnnotationView,也就是UI。而每一個MKAnnotationView都需要有对应的资料 MKAnnotation,这是Protocal,也就是存储每個座坐标所需要用到的资料的地方。因此,我们要先建立一個使用MKAnnotation的类别。

依照iPhone开发者文件的说明。这个Protocal需要声明三个属性和一个初始化方法。三个属性分別是coordinate、title、subtitle,和一个方法initWithCoords。

下面是MKAnnotation类的代码 POI.h

#import
#import
#import

@interface POI : NSObject  {

CLLocationCoordinate2D coordinate;
NSString *subtitle;
NSString *title;
}

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,retain) NSString *subtitle;
@property (nonatomic,retain) NSString *title;

-(id) initWithCoords:(CLLocationCoordinate2D) coords;

@end

下面是MKAnnotation类的代码 POI.m

#import "POI.h"

@implementation POI

@synthesize coordinate,subtitle,title;

- (id) initWithCoords:(CLLocationCoordinate2D) coords{

self = [super init];

if (self != nil) {

coordinate = coords;

}

return self;

}

- (void) dealloc

{
[title release];
[subtitle release];
[super dealloc];
}

@end

声明了符合MKAnnotation Protocal的类别后,我们就要在Google Map上建立坐标点。在iPhone上先是Google Map的程序可以參考上一篇博文

接下來,

第一步: 我声明了一个函数createMapPoint创建坐标点。在这个用到了我们在前面声明类别POI(这个符合MKAnnotation Protocal的类别),我们实例化一个POI,接着将坐标点所需的经纬度,标题,子标题等信息都放进去。接着调用

[mapView addAnnotation:poi];

把我們所建立的POI加入地图(MKMapView)的Annotation集合中。放入集合的只是坐标点的资料,这個時候还沒有真正建立坐标点

以下是函数createMapPoint的代码:

#import "POI.h"

-(void*) createMapPoint:(MKMapView *)mapView coordinateX:(double)coorX coordinateY:(double)coorY
Title:(NSString*)title Subtitle:(NSString*)subtitle{

if(mapView!=nil){

//set POI lat and lng
CLLocationCoordinate2D p1;
POI *poi;

if(coorX && coorY){

p1.latitude=coorX;
p1.longitude = coorY;
poi = [[POI alloc] initWithCoords:p1];

if(title!=NULL)
poi.title=title;

if(subtitle!=NULL)
poi.subtitle=subtitle;

[mapView addAnnotation:poi];
[poi release];

}

}
return NULL;
}

第二部:参考MKMapView的说明文件可以看到viewForAnnotation这个方法,这是MKMapView实际建立坐标点的地方。MKMapView类别在渲染地图的时候会按照Annotation集合中的资料建立坐标点。

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

//方法一:using default pin as a PlaceMarker to display on map
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
newAnnotation.pinColor = MKPinAnnotationColorGreen;
newAnnotation.animatesDrop = YES;
//canShowCallout: to display the callout view by touch the pin
newAnnotation.canShowCallout=YES;

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
newAnnotation.rightCalloutAccessoryView=button;

return newAnnotation;

//方法二:using the image as a PlaceMarker to display on map
/*
MKAnnotationView *newAnnotation=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
newAnnotation.image = [UIImage imageNamed:@"icon.png"];
newAnnotation.canShowCallout=YES;
return newAnnotation;
*/
}

Annotation 集合中有几个坐标点viewForAnnotation方法就会被执行几次。因此每次viewForAnnotation被执行,我们都要实例化一个MKAnnotationView。 MKMapView接收到MKAnnotationView的实例就会将它先是在地图上。在上面代码中使用了 MKPinAnnotationView是集成自MKAnnotationView,作用就是在地图上显示一个大头钉。你可以用

annotationView.pinColor = MKPinAnnotationColorGreen;

可以设置大头钉的颜色,不过只有红色、紫色、绿色三种。

newAnnotation.canShowCallout=YES;

设定在点大头钉的时候效果。 第10行到第12行动态建立了一個DetailDisclousue类型的按钮,替这个按钮设置了一个UIControlEventTouchUpInside事件,并将它放入气泡视图AccessoryView中。最后,将建立好的坐标回传给 MapView。

被注释的方法二 是直接使用MKAnnotationView建立坐标点,并且设置它的image属性。

- (void)checkButtonTapped:(id)sender event:(id)event{

UIAlertView *tmp= [[UIAlertView alloc] initWithTitle:@"hi!" message:@"test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[tmp show];
[tmp release];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: