本篇以 MapKit 為例,完整示範如何在 iOS 中實現地圖顯示、定位、標注與地址搜尋。以下依步驟說明。
首先需要引入 MapKit 與 CoreLocation 相關類庫,並在 Info.plist 中加入定位用途說明字串:

需要添加的兩個字串為:NSLocationWhenInUseUsageDescription 與 NSLocationAlwaysUsageDescription。

[[UINavigationBar appearance] setBarTintColor:
[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]];
self.window.rootViewController = [MapViewController new];
在 MapViewController 中聲明所需屬性,包含 MKMapView、經緯度輸入框與標籤、標注按鈕、地址輸入框、查詢按鈕,以及 CLLocationManager 定位管理器。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.locManager = [[CLLocationManager alloc] init];
_locManager.delegate = self;
_locManager.desiredAccuracy = kCLLocationAccuracyBest; // 定位精度
_locManager.distanceFilter = 10.0; // 定位頻率:10 米一次
[self.locManager requestAlwaysAuthorization];
[self.locManager startUpdatingLocation];
[self countDistance]; // 查詢兩地點間的距離
[self configureView]; // 布置視圖
[self setMapView]; // 建立地圖
}
布置視圖時,加入經度與緯度輸入框、放置標注按鈕、地址輸入框與查詢按鈕;並可透過 CLLocation 計算兩座標之間的距離:
- (void)countDistance {
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:34 longitude:113];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:35 longitude:113];
CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
NSLog(@"(%@) 和 (%@) 的距離為: %f", loc1, loc2, distance);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = locations.lastObject;
NSLog(@"經度 == %f, 維度 == %f", location.coordinate.longitude, location.coordinate.latitude);
self.longitudetext.text = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
self.latitudeText.text = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
// 不用定位服務時應立即停止,因為非常耗電
// [manager stopUpdatingLocation];
}
建立地圖視圖時可選擇標準、衛星或混合地圖類型,並透過 showsUserLocation 顯示當前位置:
- (void)setMapView {
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 100, 320, 460)];
[self.mapView setMapType:MKMapTypeStandard];
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
}
依輸入的經緯度放置標注並放大到該位置:
- (void)annotationAction:(UIButton *)sender {
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(
[[self.latitudeText text] floatValue], [[self.longitudetext text] floatValue]);
NSString *title = [NSString stringWithFormat:@"%f,%f", location.latitude, location.longitude];
Mypoint *myPoint = [[Mypoint alloc] initWithCoordinate:location andTitle:title];
[self.mapView addAnnotation:myPoint];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 0.01, 0.01);
[self.mapView setRegion:region];
}
透過 CLGeocoder 進行地理編碼,將地址字串轉換為座標:
- (void)detailSearchAction:(UIButton *)search {
if (_destination.text == nil || [_destination.text length] == 0) {
return;
}
CLGeocoder *geocode = [[CLGeocoder alloc] init];
[geocode geocodeAddressString:_destination.text completionHandler:^(NSArray *placemarks, NSError *error) {
if (error || placemarks.count == 0) {
NSLog(@"地址不存在");
} else {
CLPlacemark *firstPlacemark = [placemarks firstObject];
CLLocationDegrees latitude = firstPlacemark.location.coordinate.latitude;
CLLocationDegrees longitude = firstPlacemark.location.coordinate.longitude;
self.latitudeText.text = [NSString stringWithFormat:@"%.2f", latitude];
self.longitudetext.text = [NSString stringWithFormat:@"%.2f", longitude];
[self showLocation];
}
}];
}
最後,自訂的標注類別 Mypoint 需遵循 MKAnnotation 協定,並實作 initWithCoordinate:andTitle: 初始化方法:
@interface Mypoint : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
- (id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t;
@end
@implementation Mypoint
- (id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t {
if (self = [super init]) {
_coordinate = c;
_title = t;
}
return self;
}
@end
最終效果如下:
