文/Corwien
先匯入框架:

再匯入主標頭檔:
#import <MapKit/MapKit.h>
備記:MapKit 框架中所有資料型別的前綴都是 MK;其中最重要的 UI 控件是 MKMapView,專門用於地圖顯示。

設定 MKMapView 的 userTrackingMode 屬性即可:
MKUserTrackingModeNone:不追蹤。MKUserTrackingModeFollow:追蹤並顯示當前位置。MKUserTrackingModeFollowWithHeading:追蹤並顯示當前位置,地圖會隨使用者朝向旋轉。
那個藍色發光圓點就是使用者位置,術語上稱為「大頭針」。
要提醒的是,使用定位功能必須先在 Info.plist 中請求權限描述(NSLocationWhenInUseUsageDescription),並在執行期呼叫 requestWhenInUseAuthorization。缺少描述字串會直接導致 App 在請求權限時崩潰。
透過 mapType 設定:
MKMapTypeStandard:普通地圖(左圖)MKMapTypeSatellite:衛星影像(中圖)MKMapTypeHybrid:普通地圖疊加於衛星影像之上(右圖)
MKMapView 可設定代理物件以監聽地圖行為。常見方法有三個。
位置更新(呼叫頻繁,每次傳入最新位置):
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation;
顯示區域即將改變:
- (void)mapView:(MKMapView *)mapView
regionWillChangeAnimated:(BOOL)animated;
顯示區域已改變:
- (void)mapView:(MKMapView *)mapView
regionDidChangeAnimated:(BOOL)animated;
MKUserLocation 本身就是一個大頭針模型,包含標題、子標題與位置資訊:
@property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subtitle; @property (readonly, nonatomic) CLLocation *location;
設定中心點:
@property (nonatomic) CLLocationCoordinate2D centerCoordinate;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate
animated:(BOOL)animated;
設定顯示區域:
@property (nonatomic) MKCoordinateRegion region; - (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated;
MKCoordinateRegion 是表示區域的結構體:
typedef struct {
CLLocationCoordinate2D center; // 區域中心點
MKCoordinateSpan span; // 區域跨度
} MKCoordinateRegion;
其中 MKCoordinateSpan 定義如下:
typedef struct {
CLLocationDegrees latitudeDelta; // 緯度跨度
CLLocationDegrees longitudeDelta; // 經度跨度
} MKCoordinateSpan;
什麼是大頭針?

釘在某個具體位置,用來標示該處有特定事物——例如這裡有一家餐廳。
基本操作
- (void)addAnnotation:(id<MKAnnotation>)annotation; - (void)addAnnotations:(NSArray *)annotations; - (void)removeAnnotation:(id<MKAnnotation>)annotation; - (void)removeAnnotations:(NSArray *)annotations;
參數 id<MKAnnotation> 是大頭針模型物件,用來封裝位置、標題、子標題等資料。
建立自訂模型
#import <MapKit/MapKit.h> @interface MJTuangouAnnotation : NSObject <MKAnnotation> /** 座標位置 */ @property (nonatomic, assign) CLLocationCoordinate2D coordinate; /** 標題 */ @property (nonatomic, copy) NSString *title; /** 子標題 */ @property (nonatomic, copy) NSString *subtitle; @end
其中 coordinate 是 MKAnnotation 協定唯一必須實作的屬性。
加入大頭針:
MJTuangouAnnotation *anno = [[MJTuangouAnnotation alloc] init]; anno.title = @"廣州荔灣"; anno.subtitle = @"白天鵝賓館歡迎您"; anno.coordinate = CLLocationCoordinate2DMake(80, 166); [self.mapView addAnnotation:anno];
實務上往往需要自訂外觀,例如顯示一張圖片。


做法是設定 MKMapView 的代理,並實作:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation;
兩個關鍵注意事項:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation {
if (![annotation isKindOfClass:[MJTuangouAnnotation class]]) return nil;
static NSString *ID = @"tuangou";
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:ID];
annoView.canShowCallout = YES;
}
annoView.annotation = annotation;
MJTuangouAnnotation *tuangouAnnotation = annotation;
annoView.image = [UIImage imageNamed:tuangouAnnotation.icon];
return annoView;
}
MKAnnotationView 常用屬性
@property (nonatomic, strong) id<MKAnnotation> annotation; // 大頭針模型 @property (nonatomic, strong) UIImage *image; // 顯示圖片 @property (nonatomic) BOOL canShowCallout; // 是否顯示標注 @property (nonatomic) CGPoint calloutOffset; // 標注偏移量 @property (strong, nonatomic) UIView *rightCalloutAccessoryView; // 標注右側控件 @property (strong, nonatomic) UIView *leftCalloutAccessoryView; // 標注左側控件
MKPinAnnotationView
它是 MKAnnotationView 的子類,多出兩個屬性:
@property (nonatomic) MKPinAnnotationColor pinColor; // 大頭針顏色 @property (nonatomic) BOOL animatesDrop; // 首次顯示時是否從天而降
完整示意圖:

最後補一個實務建議:地圖相關的 API 在模擬器上的定位精度與真機有明顯差距,涉及定位與路線繪製的功能,務必在真機上驗證一次。