iOS 调起地图App进行导航(百度,高德,系统自带高德)URL API方式

demo:http://download.csdn.net/detail/qq_26598821/9484647

demo中有两处这样的判断

if ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]])
// -- 使用 canOpenURL:[NSURL URLWithString:@"baidumap://"] 判断不明白为什么为否。

原因是如下

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 baidumap 。

1,App 调起百度地图路线规划进行导航。百度 URL API :http://lbsyun.baidu.com/index.php?title=uri/api/ios

#pragma mark ------------------------------ 导航 - 百度  
-(void) onDaoHangForBaiDuMap  
{  
    //    百度地图如何调起APP进行导航  
//    mode  导航模式,固定为transit、driving、walking,分别表示公交、驾车和步行  
    NSString * modeBaiDu = @"driving";  
    switch (_seleIndex) {  
        case 1:  
        {  
            modeBaiDu = @"transit";  
        }  
            break;  
        case 2:  
        {  
            modeBaiDu = @"driving";  
        }  
            break;  
        case 3:  
        {  
            modeBaiDu = @"walking";  
        }  
            break;  
              
        default:  
            break;  
    }  
    NSString *url = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%lf,%lf&destination=%f,%f&mode=%@&src=公司|APP",[SingleObject shareSingleObject].currentCoordinate.latitude,[SingleObject shareSingleObject].currentCoordinate.longitude,self.location.latitude,self.location.longitude,modeBaiDu] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;  
      
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
      
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])// -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 baidumap 。  
        {  
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
        }else{  
            [[[UIAlertView alloc]initWithTitle:@"没有安装百度地图" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil] show];  
        }  
      
}  

2,App 调起高德地图路线规划进行导航。高德 URL API:http://lbs.amap.com/api/uri-api/ios-uri-explain/
LSApplicationQueriesSchemes

#pragma mark ------------------------------ 导航 - 高德  
-(void) onDaoHangForGaoDeMap  
{  
//    m 驾车:0:速度最快,1:费用最少,2:距离最短,3:不走高速,4:躲避拥堵,5:不走高速且避免收费,6:不走高速且躲避拥堵,7:躲避收费和拥堵,8:不走高速躲避收费和拥堵 公交:0:最快捷,2:最少换乘,3:最少步行,5:不乘地铁 ,7:只坐地铁 ,8:时间短  是  
//    t = 0:驾车 =1:公交 =2:步行  
      
    NSString * t = @"0";  
    switch (_seleIndex) {  
        case 1:  
        {  
            t = @"1";  
        }  
            break;  
        case 2:  
        {  
            t = @"0";  
        }  
            break;  
        case 3:  
        {  
            t = @"2";  
        }  
            break;  
              
        default:  
            break;  
    }  
    //起点  
    CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];  
    location = [location locationMarsFromBaidu];  
      
    CLLocationCoordinate2D coor =location.coordinate;  
      
    //目的地的位置  
    CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];  
    location2 = [location2 locationMarsFromBaidu];  
      
    CLLocationCoordinate2D coor2 =location2.coordinate;  
//    导航 URL:iosamap://navi?sourceApplication=%@&poiname=%@&lat=%lf&lon=%lf&dev=0&style=0",@"ABC"  
//    路径规划 URL:iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=BGVIS2&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&m=0&t=0  
    // -- 不能直接让用户进入导航,应该给用户更多的选择,所以先进行路径规划  
      
    NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%lf&slon=%lf&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0&t=%@",coor.latitude,coor.longitude, coor2.latitude,coor2.longitude,self.mainTitle,t] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
      
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) // -- 使用 canOpenURL 判断需要在info.plist 的 LSApplicationQueriesSchemes 添加 iosamap 。  
    {  
          
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];  
          
    }else{  
        [[[UIAlertView alloc]initWithTitle:@"没有安装高德地图" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil] show];  
    }  
      
      
}  

1,App 调起系统自带高德地图路线规划进行导航。

#pragma mark ------------------------------ 导航 - iosMap  
-(void) onDaoHangForIOSMap  
{  
    //起点  
    CLLocation * location = [[CLLocation alloc]initWithLatitude:[SingleObject shareSingleObject].currentCoordinate.latitude longitude:[SingleObject shareSingleObject].currentCoordinate.longitude];  
    location = [location locationMarsFromBaidu];  
      
    CLLocationCoordinate2D coor =location.coordinate;  
    MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc]                         initWithCoordinate:coor  addressDictionary:nil]];  
    currentLocation.name =@"我的位置";  
      
    //目的地的位置  
    CLLocation * location2 = [[CLLocation alloc]initWithLatitude:self.location.latitude longitude:self.location.longitude];  
    location2 = [location2 locationMarsFromBaidu];  
      
    CLLocationCoordinate2D coor2 =location2.coordinate;  
    //    CLLocationCoordinate2D coords = self.location;  
      
      
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coor2 addressDictionary:nil]];  
      
    toLocation.name = self.mainTitle;  
      
    NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil nil];  
    NSString * mode = MKLaunchOptionsDirectionsModeDriving;  
    switch (_seleIndex) {  
        case 1:  
        {  
            mode = MKLaunchOptionsDirectionsModeTransit;  
        }  
            break;  
        case 2:  
        {  
            mode = MKLaunchOptionsDirectionsModeDriving;  
        }  
            break;  
        case 3:  
        {  
            mode = MKLaunchOptionsDirectionsModeWalking;  
        }  
            break;  
              
        default:  
            break;  
    }  
    NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:mode, MKLaunchOptionsMapTypeKey: [NSNumber                                 numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };  
    //打开苹果自身地图应用,并呈现特定的item  
    [MKMapItem openMapsWithItems:items launchOptions:options];  
}  

转载:http://blog.csdn.net/qq_26598821/article/details/51087785
http://www.jianshu.com/p/bff790afffad

你可能感兴趣的:(iOS 调起地图App进行导航(百度,高德,系统自带高德)URL API方式)