IOS关于发起本机地图导航

来源:互联网 发布:历年进出口数据查询 编辑:程序博客网 时间:2024/06/05 19:59


#pragma mark -发起本机导航地图

-(void)locationByPhone{

    __blockNSString *urlScheme =@"xxx"

    __blockNSString *appName =@"应用名称"

    __blockCLLocationCoordinate2D coordinate =self.coordinate;

    UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"选择地图"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction *action = [UIAlertActionactionWithTitle:@"苹果地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        

        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];

        

        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]

                       launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

    }];

    

    [alert addAction:action];

    

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

    {

        UIAlertAction *action = [UIAlertActionactionWithTitle:@"百度地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

            

            NSString *urlString = [[NSStringstringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            

            NSLog(@"%@",urlString);

            

            [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:urlString]];

            

        }];

        

        [alert addAction:action];

    }

    

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

    {

        UIAlertAction *action = [UIAlertActionactionWithTitle:@"高德地图"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

            

            NSString *urlString = [[NSStringstringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            

            NSLog(@"%@",urlString);

            

            [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:urlString]];

            

        }];

        

        [alert addAction:action];

    }

    UIAlertAction *action1 = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    [alert addAction:action1];

    

    [self presentViewController:alert animated:YES completion:^{

        

    }];

}


0 0