Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a apple maps application with directions from my ios application

My aim is to open a map application from ios application with directions, I am able to open maps application but it is not showing directions, i have written the code as follows

 NSString *mystr=[[NSString alloc] initWithFormat:@"http://maps.apple.com/maps?saddr=Current+Location&daddr=Newyork"];
            NSURL *myurl=[[NSURL alloc] initWithString:mystr];
            [[UIApplication sharedApplication] openURL:myurl];

Can any one please help me how figure out how to pass parameters to this url and any other?

like image 801
Lakshmi Reddy Avatar asked Mar 22 '13 07:03

Lakshmi Reddy


2 Answers

CLLocationCoordinate2D coordinate =    CLLocationCoordinate2DMake(self.location.latitude,self.location.longitude);

//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placeMark];
if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    //using iOS6 native maps app
    if(_mode == 1)
    {
        [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking}];

    }
    if(_mode == 2)
    {
        [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];

    }
    if(_mode == 3)
    {
        [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeTransit}];

    }

} else{

    //using iOS 5 which has the Google Maps application
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", self.location.latitude, self.location.longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
like image 125
nerowolfe Avatar answered Oct 19 '22 23:10

nerowolfe


If you mean taking the user to the maps application based on two points, then you can do it like this:

Create an NSURL that looks like this:

NSURL *URL = [NSURL URLWithString:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f"];

You plug in your starting address and destination (in lat. and long.) appropriately. Tell your application to open the URL

[[UIApplication sharedApplication] openURL:URL];

It should take you to the maps application automatically!

like image 38
Vinodh Avatar answered Oct 20 '22 01:10

Vinodh