http://blog.iosxcode4.com/archives/74
☉限制:必須將iPhone的操作系統更新到ios3.0版本,開發使用的SDK也要是SDK 3.0才有內建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];
}

