1

The code I have. By the way, I can't get Call to work and Website does not open either!

- (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]];

I need to add destination address which for my App is = 28 Shad Thames, London, United Kingdom. Which format to write it in? because I cant get it to work and really need to sort this problem of my app real quick

holtii
  • 165
  • 2
  • 12

1 Answers1

1

You don't need to create outlets for each cell.

Is your didSelectRowAtIndexPath method called?

Then you could just do:

- (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 could also be indexPath.row, depending on how you made the tableView.

Edit: I have added the 'Get Directions' part. This checks if it is ios5 or ios6.

For ios5 I use the LocalizedCurrentLocation from this post http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

For ios6 I use the CLGeocoder to get the placemark and then open the map with it and the current location.

Remember to add CoreLocation.framework and MapKit.framework

Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
  • need 15 reputation which i dont have unfortunately, 1 last question, this implementation opens maps app, is there no way to do it in my app itself rather than launching maps app – holtii Dec 06 '12 at 11:27
  • It is possible, but you can't make the 'get directions' part. You can only show the location and the destination without the line and navigation etc. You would have to create a mapView. Here is a tutorial http://prassan-warrior.blogspot.dk/2012/11/apple-maps-on-iphone-with-mapkit-and.html – Roland Keesom Dec 06 '12 at 11:31
  • i got something similar, in my app 1 of my tabs is a mapview, which shows a place mark to 'design museum london' with disclosure button i wanted to implement the same thing maps app does where it shows information + get directions button, but wanted do it all within mapview of my app. – holtii Dec 06 '12 at 11:40
  • That is not easy to accomplish. Here is a start on maps http://www.techotopia.com/index.php/Working_with_Maps_on_the_iPhone_with_MapKit_and_the_MKMapView_Class – Roland Keesom Dec 06 '12 at 11:57
  • think I will stick with it opening the maps app although ideally would like it within my own app. its basically everything you helped me with just but just without launching maps – holtii Dec 06 '12 at 12:03
  • 1 more thing, you wouldnt happen to know how to animate a zoom to location or placemark pin? when I open my mapview it drops pin at a location, but i want it to zoom to the region I specified... – holtii Dec 06 '12 at 14:42
  • MKCoordinateRegion region; region.center.latitude = mapView.userLocation.location.coordinate.latitude; region.center.longitude = mapView.userLocation.location.coordinate.longitude; MKCoordinateSpan span; span.latitudeDelta = 0.010; span.longitudeDelta = 0.010; region.span = span; [mapView setRegion:region animated:YES]; – Roland Keesom Dec 06 '12 at 14:44
  • Play with the delta to see what zoom level you want. – Roland Keesom Dec 06 '12 at 14:45