客户端调用百度、高德、iPhone自带地图规划路线


客户端调用百度、高德、iPhone自带地图规划路线


#pragma mark--查看路线
-(void)lineSearch{
    BOOL hasBaiduMap = NO;
    BOOL hasGaodeMap = NO;
    
    
    NSMutableArray *titleArr =[NSMutableArray array];
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){
        hasBaiduMap = YES;
        [titleArr addObject:@"使用百度地图"];
    }
    if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
        hasGaodeMap = YES;
        [titleArr addObject:@"使用高德地图"];
    }
    [titleArr addObject:@"使用自带地图"];
    
    NSString *firstStr = titleArr[0];
    NSString *secondStr = titleArr.count > 1 ? titleArr[1] : nil;
    NSString *thirdStr = titleArr.count > 2 ? titleArr[2] : nil;
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择地图" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:firstStr, secondStr, thirdStr, nil];
    
    [sheet showInView:self.view];
}

#pragma mark--
#pragma mark--UIActionSheet delegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSString *actionTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    
    CLLocationCoordinate2D destinationCoordinate = CLLocationCoordinate2DMake(31.1594320000,121.4358650000);
    CLLocationCoordinate2D startCoordinate = CLLocationCoordinate2DMake(currentLocation.location.coordinate.latitude, currentLocation.location.coordinate.longitude);
    
    if ([actionTitle isEqualToString:@"使用百度地图"]){
        
        NSString *sName = @"我的位置";
        CGFloat sLatitude = destinationCoordinate.latitude;
        CGFloat sLongitude = destinationCoordinate.longitude;
        
        NSString *dName = @"目的地";
        CGFloat dLatitude = startCoordinate.latitude;
        CGFloat dLongitude = startCoordinate.longitude;
        
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving",sLatitude, sLongitude,sName,dLatitude, dLongitude,dName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
    }
    else if ([actionTitle isEqualToString:@"使用高德地图"]){
        
        
        
        NSString *applicationName = [HSFunction getAppDisplayName];
        NSString *sName = @"我的位置";
        CGFloat sLatitude = destinationCoordinate.latitude;
        CGFloat sLongitude = destinationCoordinate.longitude;
        
        NSString *dName = @"目的地";
        CGFloat dLatitude = startCoordinate.latitude;
        CGFloat dLongitude = startCoordinate.longitude;
        
        NSString *urlString2 = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=%@&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",applicationName,sLatitude,sLongitude,sName,dLatitude,dLongitude,dName]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString2]];
        
    }
    else if ([actionTitle isEqualToString:@"使用自带地图"]){//直接调用ios自己带的apple map
        
        //当前的位置
        MKMapItem *currentLoca = [MKMapItem mapItemForCurrentLocation];
        
        //目的地的位置
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:destinationCoordinate addressDictionary:nil]];
        toLocation.name = @"目的地";
        
        NSArray *items = [NSArray arrayWithObjects:currentLoca, toLocation, nil];
        
        NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };
        
        //打开苹果自身地图应用,并呈现特定的item
        [MKMapItem openMapsWithItems:items launchOptions:options];
        
    }
    else{
    }
}


智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告