简体   繁体   中英

iOS Options when opening maps app by pressing a UIButton?

I'm having a simple button that when pressed redirects you to the native map app in IOS 7. I am currently using the maps.google.com url way, which directs you to the safari app.

My question is, how do i make the phone ask you which map app to open the specified address, to choose between safari, google maps, apple maps or any other app on the phone? The address is a normal one, for example London, UK. Thanks!

I suggest you use custom URLs, like this:

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

Apple Maps

In order to open Apple built-in maps use this solution: Programmatically open Maps app in iOS 6

Google Maps

If you'd like to open official Google Maps app, you can use Custom URL Scheme: https://developers.google.com/maps/documentation/ios/urlscheme , like this:

if ([[UIApplication sharedApplication] canOpenURL:
     [NSURL URLWithString:@"comgooglemaps://"]]) {
  [[UIApplication sharedApplication] openURL:
   [NSURL URLWithString:@"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic"]];
} else {
  NSLog(@"Can't use comgooglemaps://");
}

How to prompt user with choise?

I suggest you using UIActionSheet: https://developer.apple.com/library/ios/documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

Check out this piece of code:

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressString];
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM