iOS 调用地图导航

在IOS6.0系统后,兼容iOS5.0与iOS6.0地图导航,需要分两个步骤

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)//用来获取手机的系统,判断系统是多少

  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、高德等

使用前,先判断设备上是否已安装应用

百度地图:

 

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

参考

 

高德地图:

 

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

参考

 

Google Maps:

 

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

参考

 

 

示例代码

 

  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

  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

    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. }

iOS 调用地图导航,布布扣,bubuko.com

iOS 调用地图导航

上一篇:Framework7:不会Objective-C,也能开发iOS7应用


下一篇:Android中shape的使用