在 iOS 上實現導航,通常有三條路徑:調起系統自帶地圖 App、向服務器請求路線數據自行繪製,或借助第三方 SDK(如百度)。本文以 MapKit 為例,完整演示前兩種做法。
點擊開始導航後,先取得使用者輸入的起點與終點,利用 CLGeocoder 進行地理編碼取得地標對象(CLPlacemark),再由 CLPlacemark 創建 MKPlacemark,進而生成 MKMapItem。起點與終點邏輯一致。
方案一:直接調用系統地圖 App 導航。方案二:向蘋果服務器請求路線信息(MKDirectionsRequest / MKDirectionsResponse),再自行繪製。
// ViewController.m // 利用系統 App 導航
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
- (IBAction)startNavigation;
@property (weak, nonatomic) IBOutlet UITextField *startField;
@property (weak, nonatomic) IBOutlet UITextField *endField;
@property(nonatomic, strong) CLGeocoder *geocoder;
@end
@implementation ViewController
- (IBAction)startNavigation {
NSString *startStr = self.startField.text;
NSString *endStr = self.endField.text;
if (startStr == nil || startStr.length == 0 || endStr == nil || endStr.length == 0) {
NSLog(@"請輸入起點或者終點"); return;
}
[self.geocoder geocodeAddressString:startStr completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == 0) return;
CLPlacemark *startCLPlacemark = [placemarks firstObject];
[self.geocoder geocodeAddressString:endStr completionHandler:^(NSArray *placemarks, NSError *error) {
if (placemarks.count == 0) return;
CLPlacemark *endCLPlacemark = [placemarks firstObject];
[self startNavigationWithstartCLPlacemark:startCLPlacemark endCLPlacemark:endCLPlacemark];
}];
}];
}
- (void)startNavigationWithstartCLPlacemark:(CLPlacemark *)startCLPlacemark endCLPlacemark:(CLPlacemark *)endCLPlacemark {
MKPlacemark *startPlacemark = [[MKPlacemark alloc] initWithPlacemark:startCLPlacemark];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startPlacemark];
MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithPlacemark:endCLPlacemark];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endPlacemark];
NSArray *items = @[startItem, endItem];
NSMutableDictionary *md = [NSMutableDictionary dictionary];
md[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving; //駕車模式
//md[MKLaunchOptionsMapTypeKey] = @(MKMapTypeHybrid);
//調用 open 方法即可打開系統地圖 App 進行導航
[MKMapItem openMapsWithItems:items launchOptions:md];
}
- (CLGeocoder *)geocoder {
if (!_geocoder) { self.geocoder = [[CLGeocoder alloc] init]; }
return _geocoder;
}
@end
MKDirectionsRequest 說清楚「從哪裡到哪裡」,MKDirectionsResponse 則承載具體路線信息:
- (void)startDirectionsWithstartCLPlacemark:(CLPlacemark *)startCLPlacemark endCLPlacemark:(CLPlacemark *)endCLPlacemark {
MKPlacemark *startMKPlacemark = [[MKPlacemark alloc] initWithPlacemark:startCLPlacemark];
MKMapItem *startItem = [[MKMapItem alloc] initWithPlacemark:startMKPlacemark];
MKPlacemark *endMKPlacemark = [[MKPlacemark alloc] initWithPlacemark:endCLPlacemark];
MKMapItem *endItem = [[MKMapItem alloc] initWithPlacemark:endMKPlacemark];
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = startItem;
request.destination = endItem;
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
NSArray *routes = response.routes;
for (MKRoute *route in routes) {
NSLog(@"%f千米 %f小時", route.distance / 1000, route.expectedTravelTime / 3600);
NSArray *steps = route.steps;
for (MKRouteStep *step in steps) {
NSLog(@"%@ %f", step.instructions, step.distance);
}
}
}];
}
在取得路線後,可透過 MKMapView 的代理把 route.polyline 作為遮蓋添加到地圖上,由系統自動繪製,並在 rendererForOverlay 中設置線寬與顏色:
#pragma mark - MKMapViewDelegate
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
//注意:創建線條時一定要指定幾何路線
MKPolylineRenderer *line = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
line.lineWidth = 1;
line.strokeColor = [UIColor redColor];
return line;
}
把路線遮蓋添加到地圖:
[self.mapVIew addOverlay:route.polyline];
如此一來,三種導航方案便各有其適用場景:追求簡單直接可調起系統地圖,需要自定義界面則請求數據自行繪製,而追求成熟導航體驗則可引入第三方 SDK。