开发者

Go to iPhone maps app from another app

开发者 https://www.devze.com 2023-03-28 04:55 出处:网络
I\'m writing an app where I want the user to be able to get directions. I know about the google API, and I have that implemented, but I was wondering about ways开发者_StackOverflow社区 to jump directl

I'm writing an app where I want the user to be able to get directions. I know about the google API, and I have that implemented, but I was wondering about ways开发者_StackOverflow社区 to jump directly to the native Maps app and give the user directions instead. I think I've seen it done before, but I could be mistaken.

Any help is much appreciated.


You can open the maps app by using regular URL that points to Google Maps. From the Apple URL Scheme reference:

The maps URL scheme is used to show geographical locations and to generate driving directions between two points. If your application includes address or location information, you can use map links to forward that information to the Maps application on iOS and the Google Maps website on other platforms.

Unlike some schemes, map URLs do not start with a “maps” scheme identifier. Instead, map links are specified as regular http links but are targeted at the Google Maps servers. The following examples show the strings you would use in Safari and in a native application to show a map of the city of Cupertino, California.

  • HTML link:

<a href="http://maps.google.com/maps?q=cupertino">Cupertino</a>

Note that on a device that does not have maps installed (not sure if those even exist), the link will open in a regular browser, since it is simply an HTTP link.

To open the URL, you can present the link in a UIWebView, or you can use code to open it. for example:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=Cupertino"]];

If you wanted to pass in a custom location and open it when the user taps a button, you can make a method like this:

- (IBAction) openMapsAppAndShowLocation:(NSString *)locationToShow{

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", locationToShow]]];

}

If you wanted, you could refactor the method to take latitude/longitude pair too. That might look like this:

- (IBAction) openMapsAppAndShowLatitude:(double)latitude andLongitude:(double)longitude{

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?q=\"%f,%f\"", latitide, longitude]]];

}


You can use either this:

NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

this,

UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];

or this

[someUIApplication openURL:[[NSURL alloc] initWithString: @"http://maps.google.com/maps?q=London"]]

You can even specify the zoom level using the z flag (values between 1-19):

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];

That should invoke the Google Maps app. I think that these instructions only work on the actual device and NOT in the simulator. Good luck and comment below if you have any problem with the snippets :)


Very simple

UIApplication *app = [UIApplication sharedApplication];  
[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]; 

Easy as a kiss


@Moshe

Note that on a device that does not have maps installed (not sure if those even exist)

Take an iPhone simulator for example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号