1

我有的代码。顺便说一句,我无法接听上班电话,网站也无法打开!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlToOpen = @"";
if( indexPath.section == 0 ){
    // call number
    urlToOpen = @"tel:442074036933";
} else if( indexPath.section == 1 ){
    // open website
    urlToOpen = @"http://www.designmuseum.org";
} else {
    // open address

    urlToOpen = @"http://maps.apple.com/maps?daddr=Design+Museum+London";
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
NSString *destinationAddress = [NSString stringWithFormat:@"%@+%@+%@",
                                [address street],
                                [address city],
                                [address country]];

NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];

NSString *url = [NSString stringWithFormat:@"http://maps.apple.com/maps?saddr=%@&daddr=%@",
                 [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                 [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

我需要为我的应用添加目标地址 = 28 Shad Thames, London, United Kingdom。用什么格式写?因为我无法让它工作并且真的需要快速解决我的应用程序的这个问题

4

1 回答 1

1

您不需要为每个单元创建插座。

你的 didSelectRowAtIndexPath 方法被调用了吗?

然后你可以这样做:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *urlToOpen = @"";
    if( indexPath.section == 0 ){
        // call number
        urlToOpen = @"tel:12125551212";
    } else if( indexPath.section == 1 ){
        // open website
        urlToOpen = @"http://www.google.nl";
    } else {
        // open address
            NSString *destinationAddress = @"Amsterdam";

            Class itemClass = [MKMapItem class];
            if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {

                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
                [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) {
                    if([placemarks count] > 0) {

                        MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];

                        MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark];

                        MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation];


                        NSArray *mapItems = @[mapItem, mapItem2];

                        NSDictionary *options = @{
                                        MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                        MKLaunchOptionsMapTypeKey:
                                        [NSNumber numberWithInteger:MKMapTypeStandard],
                                        MKLaunchOptionsShowsTrafficKey:@YES
                        };

                        [MKMapItem openMapsWithItems:mapItems launchOptions:options];

                    } else {
                        //error nothing found
                    }
                }];
                return;
            } else {

                NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage];

                urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
                             [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                             [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            }
    }
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
}

indexPath.section 也可以是 indexPath.row,具体取决于您制作 tableView 的方式。

编辑: 我添加了“获取路线”部分。这将检查它是 ios5 还是 ios6。

对于 ios5,我使用这篇文章http://www.martip.net/blog/localized-current-location-string-for-iphone-apps中的 LocalizedCurrentLocation

对于 ios6,我使用 CLGeocoder 获取地标,然后用它和当前位置打开地图。

记得添加 CoreLocation.framework 和 MapKit.framework

于 2012-12-05T12:43:43.850 回答