0

I am able to display several locations or annotation pins on a map. And I display a pop up with a title when user clicks on it. how do I allow user to get directions when they click on the ">" button on the popup

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
John Rambo
  • 87
  • 10

3 Answers3

0

Code behave different on iOS 5.x and iOS 6.x. Assume the annotation is "toAnnotation", the code below will work:

- (void)openMap
{
    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6.x and later
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        MKMapItem *toLocation = [[MKMapItem alloc]
                                 initWithPlacemark:[[MKPlacemark alloc]
                                                    initWithCoordinate:CLLocationCoordinate2DMake(toAnnotation.coordinate.latitude, toAnnotation.coordinate.longitude)
                                                    addressDictionary:nil]];
        toLocation.name = toAnnotation.title;
        [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
                       launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil]
                                                                 forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
    } else { // iOS 5.1 and earlier
        NSMutableString *mapURL = [NSMutableString stringWithString:@"http://maps.google.com/maps?"];
        [mapURL appendFormat:@"saddr=Current Location"];
        [mapURL appendFormat:@"&daddr=%f,%f", toAnnotation.coordinate.latitude, toAnnotation.coordinate.longitude];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[mapURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    }
}
vietstone
  • 8,784
  • 16
  • 52
  • 79
0

firstly you need to in view controller.h file write

- (IBAction)startWithOnePlacemark:(id)sender;
- (IBAction)startWithMultiplePlacemarks:(id)sender;
- (IBAction)startInDirectionsMode:(id)sender;

then in Viewcontroller.m

- (IBAction)startWithOnePlacemark:(id)sender
{
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300);
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil];
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark];
bigBenItem.name = @"Big Ben";

[bigBenItem openInMapsWithLaunchOptions:nil];

// Note: use initWithPlacemark: to initialize with CLPlacemark
}

- (IBAction)startWithMultiplePlacemarks:(id)sender
{
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300);
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil];
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark];
bigBenItem.name = @"Big Ben";

CLLocationCoordinate2D westminsterLocation = CLLocationCoordinate2DMake(51.50054300, -0.13570200);
MKPlacemark *westminsterPlacemark = [[MKPlacemark alloc] initWithCoordinate:westminsterLocation addressDictionary:nil];
MKMapItem *westminsterItem = [[MKMapItem alloc] initWithPlacemark:westminsterPlacemark];
westminsterItem.name = @"Westminster Abbey";

NSArray *items = [[NSArray alloc] initWithObjects:bigBenItem, westminsterItem, nil];
[MKMapItem openMapsWithItems:items launchOptions:nil];
}

- (IBAction)startInDirectionsMode:(id)sender
{
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300);
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil];
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark];
bigBenItem.name = @"Big Ben";

CLLocationCoordinate2D westminsterLocation = CLLocationCoordinate2DMake(51.50054300, -0.13570200);
MKPlacemark *westminsterPlacemark = [[MKPlacemark alloc] initWithCoordinate:westminsterLocation addressDictionary:nil];
MKMapItem *westminsterItem = [[MKMapItem alloc] initWithPlacemark:westminsterPlacemark];
westminsterItem.name = @"Westminster Abbey";

NSArray *items = [[NSArray alloc] initWithObjects:bigBenItem, westminsterItem, nil];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking};
[MKMapItem openMapsWithItems:items launchOptions:options];
}

add Mapkit,AddressBook,CoreLocation Framework

user19634
  • 15
  • 1
  • 8
0

You'll need to do two things: 1. Get the location from the map pin 2. Open the Google Maps URL.

Here's how it works (some of this came from here)

CLLocationCoordinate2D start = myMapView.userLocation.location.coordinate;
CLLocationCoordinate2D destination = [pinSelected.annotation coordinate];        

NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
                                                                     start.latitude, start.longitude, destination.latitude, destination.longitude];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
Community
  • 1
  • 1
bryanjclark
  • 6,247
  • 2
  • 35
  • 68