Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open Maps app from my code to show directions?

Tags:

iphone

HI.. I have to show directions between two coordinates. I'd like to open Maps app, passing the start and end coordinates from my code.

I don't want to open it in the Google maps, which opens in browser(Safari). I tried that method. That was working perfect.

NSString* urlStr = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", s_lat, s_long, d_lat, d_long];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:urlStr]];

But, I want to open iPhone Maps app. How can i do this? Is this possible? Any help will be appreciated.

like image 527
EmptyStack Avatar asked Jan 03 '11 05:01

EmptyStack


2 Answers

Try it on a real device. I'm using the same code and it opens Safari at the simulator and iPhone Maps at the device.

like image 53
AlexVogel Avatar answered Oct 26 '22 23:10

AlexVogel


Hey Simon the method you are using is

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

will only open safari.So there is no chance that with open url you would open a diiferent thing.You can use the UIWebView for this and then load the webview with the url.I think that would be the simple thing like this

 views=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    [views setBackgroundColor:[UIColor lightGrayColor]];
    NSString* urlStr = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", s_lat, s_long, d_lat, d_long];

    NSURL *url=[NSURL URLWithString:urlStr];
    NSURLRequest *req=[NSURLRequest requestWithURL:url];
    //[views loadHTMLString:googlePage baseURL:requestURL];
    [views loadRequest:req];
    self.view=views;

And if you even don't want to use this then then you can use the MapKit provided in your XCode Frameworks.Hope this would help you.

like image 40
Sabby Avatar answered Oct 26 '22 23:10

Sabby