在 Map Kit 裡,直接加到 map view 上的子視圖不會随地圖移動——map view 會固定它們的位置。若你要放的是「釘在某一個地理座標上」的內容,就必須改用 annotations 與 overlays。
兩者的差別在於:annotation 用來標示由「一組經緯度」定義的單一位置;overlay 則是由多個點定義、或涵蓋許多連續圖形的區域(例如一段路線、一個多边形)。
annotation object(注釋物件):通常是一些小型資料物件,保存地圖座標與相關資訊。
annotation view(注釋視圖):負責把上述資料畫出來。Map Kit 提供了幾種標準的注釋視圖,你也可以自訂。但要特別注意:不能把注釋視圖直接 addSubview 到 map view 上,必須透過 map view 的 delegate 物件來提供。
1. 定義注釋物件。如果只需要標題與座標,用 MKPointAnnotation 即可,它能顯示標題與副標題。若要儲存任何類型的自訂資料,就實作一個遵守 MKAnnotation 協議的物件。
2. 定義注釋視圖。如果一張靜態圖片就夠,建立 MKAnnotationView 的實例並把圖 assign 給 image 屬性;想用系統標準的圖釘樣式,建立 MKPinAnnotationView 的實例;若靜態圖片不足以表達,就繼承 MKAnnotationView 自繪。
3. 在 delegate 中實作 mapView:viewForAnnotation:。這個方法應該先嘗試用 dequeueReusableAnnotationViewWithIdentifier: 取回既有視圖,沒有的話才建立新的。若你的 App 支援多種注釋,就要在這裡判斷型別並回傳對應的視圖。
4. 把注釋物件加入 map view。用 addAnnotation: 或 addAnnotations:。
注釋物件只要遵守 MKAnnotation 協議即可。若只需標題與座標,直接用 MKPointAnnotation;需要更多信息時才自訂。
@interface MyCustomAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithLocation:(CLLocationCoordinate2D)coord;
// Other methods and properties.
@end
@implementation MyCustomAnnotation
@synthesize coordinate;
- (id)initWithLocation:(CLLocationCoordinate2D)coord {
self = [super init];
if (self) {
coordinate = coord;
}
return self;
}
@end
MKAnnotationView 定義了注釋視圖的基本特性;MKPinAnnotationView 是它的子類,用來顯示系統標準的 pin 樣式。
建立 MKAnnotationView 實例並設定 image 屬性後,該圖就會顯示在對應座標上。若你不希望圖片以座標為中心,可以用 centerOffset 在水平與垂直方向任意位移。
MKAnnotationView *aView =
[[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"MyCustomAnnotation"] autorelease];
aView.image = [UIImage imageNamed:@"myimage.png"];
aView.centerOffset = CGPointMake(10, -20);
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MyCustomAnnotationView : MKAnnotationView
{
// Custom data members
}
// Custom properties and methods.
@end
初始化時要設定合適的尺寸,並把 opaque 設為 NO,讓地圖內容能透出未被繪製的部分:
// Listing 5-5 Initializing a custom annotation view
- (id)initWithAnnotation:(id <MKAnnotation>)annotation
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self)
{
// 設定合適的尺寸
CGRect myFrame = self.frame;
myFrame.size.width = 40;
myFrame.size.height = 40;
self.frame = myFrame;
// opaque 預設是 YES,設為 NO 可讓地圖內容透出來
self.opaque = NO;
}
return self;
}
// Listing 5-6 Creating annotation views
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
// 使用者位置不用自訂,直接回傳 nil
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// 處理自訂注釋
if ([annotation isKindOfClass:[MyCustomAnnotation class]])
{
// 先嘗試取回可重用的視圖
MKPinAnnotationView *pinView = (MKPinAnnotationView *)
[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if (!pinView)
{
// 沒有現成的就建立一個
pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:@"CustomPinAnnotation"] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
// 在氣泡上加一顆細節披露按鈕
UIButton *rightButton = [UIButton buttonWithType:
UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(myShowDetailsMethod:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
return nil;
}
當 map view 需要顯示注釋時,會呼叫 delegate 的 mapView:viewForAnnotation:。如果你不實作這個方法、或總是回傳 nil,map view 會使用預設的注釋視圖,也就是 pin annotation view。
建立新視圖之前,記得先檢查是否已有同類視圖可重用(dequeueReusableAnnotationViewWithIdentifier:),這個思路和 UITableView 的 cell 完全一樣,都是為了避免大量注釋時不斷配置與銷毀視圖。
為了避免注釋視圖重疊,可以在 mapView:regionWillChangeAnimated: 與 mapView:regionDidChangeAnimated: 這兩個 delegate 方法中,依縮放等級或可視範圍動態增減注釋。例如地圖縮得很遠時只顯示重要地標,放大後再補上細節。
兩步即可:在注釋物件中實作 setCoordinate: 方法,讓座標可被更新;建立注釋視圖時把 draggable 屬性設為 YES。
摘自 nerohoop的專欄