IOS 8 使用系統自帶導航

jopen 9年前發布 | 2K 次閱讀 Objective-C IOS

//
//  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</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!