Last active
August 29, 2015 14:26
-
-
Save Narmo/d6c29ab90e247697f080 to your computer and use it in GitHub Desktop.
Open location in external maps app (iOS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Open location in one of external maps app: Google Maps, Yandex.Navigator, Yandex.Maps, Maps (iOS) | |
| @param location Coordinates in following format: "xx.yyy,xx.yyy" (longitude,latitude) | |
| */ | |
| + (void)openLocation:(NSString *)location { | |
| if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { | |
| NSArray *coordinatesComponents = [location componentsSeparatedByString:@","]; | |
| CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake([coordinatesComponents[0] doubleValue], [coordinatesComponents[1] doubleValue]); | |
| NSString *googleMapsUrlString = [NSString stringWithFormat:@"comgooglemaps://?q=%@", location]; | |
| NSString *yandexNaviUrlString = [NSString stringWithFormat:@"yandexnavi://build_route_on_map?lat_to=%f&lon_to=%f", coordinates.latitude, coordinates.longitude]; | |
| NSString *yandexMapsUrlString = [NSString stringWithFormat:@"yandexmaps://maps.yandex.ru/?ll=%f,%f5&z=12", coordinates.longitude, coordinates.latitude]; | |
| NSURL *googleMapsUrl = [NSURL URLWithString:googleMapsUrlString]; | |
| NSURL *yandexNaviUrl = [NSURL URLWithString:yandexNaviUrlString]; | |
| NSURL *yandexMapsUrl = [NSURL URLWithString:yandexMapsUrlString]; | |
| BOOL hasGoogleMaps = [[UIApplication sharedApplication] canOpenURL:googleMapsUrl]; | |
| BOOL hasYandexNavi = [[UIApplication sharedApplication] canOpenURL:yandexNaviUrl]; | |
| BOOL hasYandexMaps = [[UIApplication sharedApplication] canOpenURL:yandexMapsUrl]; | |
| if (hasGoogleMaps || hasYandexNavi || hasYandexMaps) { | |
| UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; | |
| if (hasGoogleMaps) { | |
| UIAlertAction *googleMapsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Google Maps", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { | |
| [[UIApplication sharedApplication] openURL:googleMapsUrl]; | |
| }]; | |
| [alert addAction:googleMapsAction]; | |
| } | |
| if (hasYandexMaps) { | |
| UIAlertAction *yandexMapsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yandex.Maps", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { | |
| [[UIApplication sharedApplication] openURL:yandexMapsUrl]; | |
| }]; | |
| [alert addAction:yandexMapsAction]; | |
| } | |
| if (hasYandexNavi) { | |
| UIAlertAction *yandexNaviAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Yandex.Navigator", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { | |
| [[UIApplication sharedApplication] openURL:yandexNaviUrl]; | |
| }]; | |
| [alert addAction:yandexNaviAction]; | |
| } | |
| UIAlertAction *iosMapsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"iOS maps", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { | |
| [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.apple.com/?q=%@", location]]]; | |
| }]; | |
| [alert addAction:iosMapsAction]; | |
| UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"") style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}]; | |
| [alert addAction:cancelAction]; | |
| APAppDelegate *appDelegate = (APAppDelegate *)[UIApplication sharedApplication].delegate; | |
| UIViewController *currentViewController = appDelegate.currentViewController; | |
| if (currentViewController == nil) { | |
| UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; | |
| while (topController.presentedViewController) { | |
| topController = topController.presentedViewController; | |
| } | |
| currentViewController = topController; | |
| } | |
| [currentViewController presentViewController:alert animated:YES completion:nil]; | |
| } | |
| else { | |
| NSString *externalUrlStr = [NSString stringWithFormat:@"http://maps.apple.com/?q=%@", location]; | |
| [[UIApplication sharedApplication] openURL:[NSURL URLWithString:externalUrlStr]]; | |
| } | |
| } | |
| else { | |
| NSString *externalUrlStr = [NSString stringWithFormat:@"http://maps.apple.com/?q=%@", location]; | |
| [[UIApplication sharedApplication] openURL:[NSURL URLWithString:externalUrlStr]]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment