標籤:
//// ViewController.m// APP內建導航//// Created by wup on 15/5/23.// Copyright (c) 2015年 apple. All rights reserved.//#import "ViewController.h"#import <MapKit/MapKit.h>@interface ViewController ()@property (nonatomic,strong) CLGeocoder *geo;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.// CLLocationManager *clmgr = [[CLLocationManager alloc] init];// [clmgr requestAlwaysAuthorization]; // MKMapView *mv = [[MKMapView alloc] initWithFrame:self.view.bounds];// [self.view addSubview:mv]; [self.geo geocodeAddressString:@"麗江" completionHandler:^(NSArray *placemarks, NSError *error) { //擷取到起點的MKplaceMark MKPlacemark *startPlace = [[MKPlacemark alloc] initWithPlacemark:[placemarks firstObject]]; //等待擷取到起點的placemarks之後在擷取終點的placemarks,block回調延遲問題 [self.geo geocodeAddressString:@"北京" completionHandler:^(NSArray *placemarks, NSError *error) { /** 擷取到終點的MKplaceMark,MKPlaceMark 是ClPlaceMark的子類。 */ MKPlacemark *endPlace = [[MKPlacemark alloc] initWithPlacemark:[placemarks firstObject]]; /** 將MKPlaceMark轉換成MKMapItem,這樣可以放入到item這個數組中 */ MKMapItem *startItem = [[MKMapItem alloc ] initWithPlacemark:startPlace]; MKMapItem *endItem = [[MKMapItem alloc ] initWithPlacemark:endPlace]; NSArray *item = @[startItem ,endItem]; //建立字典儲存導航的相關參數 NSMutableDictionary *md = [NSMutableDictionary dictionary]; md[MKLaunchOptionsDirectionsModeKey] = MKLaunchOptionsDirectionsModeDriving; md[MKLaunchOptionsMapTypeKey] = [NSNumber numberWithInteger:MKMapTypeHybrid]; /** *調用app內建導航,需要傳入一個數組和一個字典,數組中放入MKMapItem, 字典中放入對應索引值 MKLaunchOptionsDirectionsModeKey 開啟導航模式 MKLaunchOptionsMapTypeKey 地圖模式 MKMapTypeStandard = 0, MKMapTypeSatellite, MKMapTypeHybrid // 導航模式 MKLaunchOptionsDirectionsModeDriving 開車; MKLaunchOptionsDirectionsModeWalking 步行; */#warning 其實所有的代碼都是為了下面一句話,開啟系統內建的高德地圖然後執行某些動作,launchOptions裡面的參數指定做哪些動作 [MKMapItem openMapsWithItems:item launchOptions:md]; }]; }]; }#pragma mark - 超級懶載入-(CLGeocoder *)geo{ if (!_geo) { _geo = [[CLGeocoder alloc] init]; } return _geo;}@end
IOS 8 使用系統內建導航