3

I want to show maps & draw routes on maps. My application supports for ios 4 plus. So how should i use maps to work on ios 6 as well as before. Also i want to know sholud i use custom mapview in my app to display maps & routes or should i use

[[UIApplication sharedApplication] openURL:]

I have never user MapKits. So please provide if any tutorial. Also let me know if there are any rd party libraries that can be used.

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Sayali
  • 593
  • 3
  • 9
  • 22

2 Answers2

5

If you don't want an in-app map. Use the following:

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

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

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
}

This opens the map application and 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

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
  • hi there. can u give me an idea on how can i get an in-map app depicting the navigation routes and driving directions...Please help. because i used ur above code but it takes me out of my native app and i can't cum back to my app then after checking the route or map.. Thankss!! – nikesh Feb 12 '13 at 05:40
  • First line of my post: 'If you don't want an in-app map.' It's not easy to do an in-app navigation map. Check this post for some info http://stackoverflow.com/questions/2834523/drawing-a-route-in-mapkit-in-iphone-sdk – Roland Keesom Feb 12 '13 at 08:34