參考文檔:
iOS 跳轉(zhuǎn)方式實現(xiàn)地圖導(dǎo)航功能
應(yīng)用內(nèi)導(dǎo)航
是指使用地圖服務(wù)提供的SDK(比如高德,百度等等)住练,直接將導(dǎo)航功能嵌入到我們自己的APP內(nèi)部
但是這個方案我個人不喜歡,一是接入要一定的時間侵歇,二是增加APP的內(nèi)存占用。
一绑榴、高德導(dǎo)航(應(yīng)用內(nèi)導(dǎo)航)
1邢滑、cocoaPods 安裝高德導(dǎo)航SDK
platform :ios, "7.0" target 'yourTargetName' do pod 'AMapNavi' #導(dǎo)航SDK end
2、配置 Info.plist 文件窑业。
由于導(dǎo)航需要定位钦幔,需申請定位權(quán)限,在 Info.plist 中加入 NSLocationAlwaysUsageDescription 字段常柄。
3鲤氢、在 AppDelegate.m 文件中
1) 引入頭文件 #import <AMapFoundationKit/AMapFoundationKit.h>
2) 在didFinishLaunchingWithOptions 方法中配置高德 Key。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
……
[AMapServices sharedServices].apiKey = "您的Key";
……
}
4西潘、在要跳轉(zhuǎn)到高德導(dǎo)航地圖的視圖控器中
1)倒入頭文件#import <AMapNaviKit/AMapNaviKit.h>
2)創(chuàng)建屬性:compositeManager
@property (nonatomic, strong) AMapNaviCompositeManager *compositeManager;
3)在跳轉(zhuǎn)的點擊方法中
[cell.mapBtn addActionHandler:^(NSInteger tag) {
NSLog(@"點擊了地圖");
// 初始化
self.compositeManager = [[AMapNaviCompositeManager alloc] init];
// 如果需要使用AMapNaviCompositeManagerDelegate的相關(guān)回調(diào)(如自定義語音卷玉、獲取實時位置等),需要設(shè)置delegate
// self.compositeManager.delegate = weakSelf;
// 通過present的方式顯示路線規(guī)劃頁面, 在不傳入起終點啟動導(dǎo)航組件的模式下喷市,options需傳入nil
// [self.compositeManager presentRoutePlanViewControllerWithOptions:nil];
AMapNaviCompositeUserConfig *config = [[AMapNaviCompositeUserConfig alloc] init];
[config setRoutePlanPOIType:AMapNaviRoutePlanPOITypeEnd location:[AMapNaviPoint locationWithLatitude:[self.model.store_y floatValue] longitude:[self.model.store_x floatValue]] name:self.model.title POIId:nil]; //傳入終點
[self.compositeManager presentRoutePlanViewControllerWithOptions:config];
}];
應(yīng)用外導(dǎo)航
是以URI跳轉(zhuǎn)的方式(在iOS中就是以URL Scheme的方式)相种,直接跳到對應(yīng)的地圖APP中,直接利用對方的功能來導(dǎo)航品姓。
這樣的優(yōu)點寝并,一是接入方便,二是不增加自己APP的開銷缭黔;缺點就是如果用戶沒有裝這個地圖應(yīng)用就沒辦法使用這個地圖的服務(wù)食茎。
一、跳轉(zhuǎn)到蘋果自帶地圖
1馏谨、蘋果提供了另一種方式:MKMapItem(要使用記得導(dǎo)入#import <MapKit/MapKit.h> 頭文件)
2别渔、實現(xiàn)代碼:
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([self.model.latitude floatValue], [self.model.longitude floatValue]);
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:loc addressDictionary:nil]];
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
toLocation.name =self.model.title;//終點地址名稱