iOS 調用地圖導航

注意:本文章下的代碼有個別變量未知,因此是不能直接跑通的,我也是轉別人的ios

在IOS6.0系統後,兼容iOS5.0與iOS6.0地圖導航,須要分兩個步驟git

 

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用來獲取手機的系統,判斷系統是多少api

[cpp]  view plain copy
 
  1. CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;  
  2. CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);  
  3.   
  4. if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6如下,調用google map  
  5.       
  6.     NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];  
  7.     //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude  
  8.     urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  9.     NSURL *aURL = [NSURL URLWithString:urlString];  
  10.     [[UIApplication sharedApplication] openURL:aURL];  
  11. else { // 直接調用ios本身帶的apple map  
  12.       
  13.     MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];  
  14.     MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil]];  
  15.     toLocation.name = @"to name";  
  16.       
  17.     [MKMapItem openMapsWithItems:@[currentLocation, toLocation]  
  18.                    launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];  
  19.       
  20. }  

 

若是不想使用蘋果自帶的地圖的話,也可使用第三方的地圖,如百度、Google Maps、高德等app

使用前,先判斷設備上是否已安裝應用ide

百度地圖:ui

 

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"baidumap://map/"]])google

參考url

 

高德地圖:spa

 

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"iosamap://"]]).net

參考

 

Google Maps:

 

if ([[UIApplicationsharedApplication]canOpenURL:[NSURLURLWithString:@"comgooglemaps://"]])

參考

 

 

示例代碼

 

[cpp]  view plain copy
 
  1. - (void)availableMapsApps {  
  2.     [self.availableMaps removeAllObjects];  
  3.       
  4.     CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;  
  5.     CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);  
  6.     NSString *toName = @"to name";  
  7.       
  8.     if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){  
  9.         NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:個人位置&destination=latlng:%f,%f|name:%@&mode=transit",  
  10.                                startCoor.latitude, startCoor.longitude, endCoor.latitude, endCoor.longitude, toName];  
  11.           
  12.         NSDictionary *dic = @{@"name": @"百度地圖",  
  13.                               @"url": urlString};  
  14.         [self.availableMaps addObject:dic];  
  15.     }  
  16.     if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {  
  17.         NSString *urlString = [NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=applicationScheme&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=0&style=3",  
  18.                                @"雲華時代", endCoor.latitude, endCoor.longitude];  
  19.           
  20.         NSDictionary *dic = @{@"name": @"高德地圖",  
  21.                               @"url": urlString};  
  22.         [self.availableMaps addObject:dic];  
  23.     }  
  24.     if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {  
  25.         NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?saddr=&daddr=%f,%f¢er=%f,%f&directionsmode=transit", endCoor.latitude, endCoor.longitude, startCoor.latitude, startCoor.longitude];  
  26.           
  27.         NSDictionary *dic = @{@"name": @"Google Maps",  
  28.                               @"url": urlString};  
  29.         [self.availableMaps addObject:dic];  
  30.     }  
  31. }  


顯示一個ActionSheet

 

[cpp]  view plain copy
 
  1. [self availableMapsApps];  
  2.     UIActionSheet *action = [[UIActionSheet alloc] init];  
  3.       
  4.     [action addButtonWithTitle:@"使用系統自帶地圖導航"];  
  5.     for (NSDictionary *dic in self.availableMaps) {  
  6.         [action addButtonWithTitle:[NSString stringWithFormat:@"使用%@導航", dic[@"name"]]];  
  7.     }  
  8.     [action addButtonWithTitle:@"取消"];  
  9.     action.cancelButtonIndex = self.availableMaps.count + 1;  
  10.     action.delegate = self;  
  11.     [action showInView:self.view];  

實現delegate

 

 

[cpp]  view plain copy
 
    1. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {  
    2.     if (buttonIndex == 0) {  
    3.         CLLocationCoordinate2D startCoor = self.mapView.userLocation.location.coordinate;  
    4.         CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake(startCoor.latitude+0.01, startCoor.longitude+0.01);  
    5.           
    6.         if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { // ios6如下,調用google map  
    7.               
    8.             NSString *urlString = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirfl=d",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude];  
    9.             //        @"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",startCoor.latitude,startCoor.longitude,endCoor.latitude,endCoor.longitude  
    10.             urlString =  [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    11.             NSURL *aURL = [NSURL URLWithString:urlString];  
    12.             [[UIApplication sharedApplication] openURL:aURL];  
    13.         } else{// 直接調用ios本身帶的apple map  
    14.               
    15.             MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];  
    16.             MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:endCoor addressDictionary:nil];  
    17.             MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];  
    18.             toLocation.name = @"to name";  
    19.               
    20.             [MKMapItem openMapsWithItems:@[currentLocation, toLocation]  
    21.                            launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];  
    22.               
    23.         }  
    24.     }else if (buttonIndex < self.availableMaps.count+1) {  
    25.         NSDictionary *mapDic = self.availableMaps[buttonIndex-1];  
    26.         NSString *urlString = mapDic[@"url"];  
    27.         urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
    28.         NSURL *url = [NSURL URLWithString:urlString];  
    29.         DEBUG_LOG(@"\n%@\n%@\n%@", mapDic[@"name"], mapDic[@"url"], urlString);  
    30.         [[UIApplication sharedApplication] openURL:url];  
    31.     }  
    32. }  
    33. 原文:http://blog.csdn.net/u011011341/article/details/9233049#
相關文章
相關標籤/搜索