// // RAMapNavigateHandler.m // APEX CRM // // Created by Jack on 2018/11/29. // Copyright © 2018年 USAI. All rights reserved. // #import "RAMapNavigateHandler.h" #import #import @implementation RAMapNavigateHandler + (void)ra_navigate:(NSString *)addr withTitle:(NSString *)title viewController:(UIViewController *)vc { if (vc) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet]; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Google Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?q=%@&directionsmode=driving",addr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) { NSLog(@"%u",success); }]; // NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?center=37.782652,-122.410126&directionsmode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; }]; [alertVC addAction:googleMapAction]; } UIAlertAction *appleMapAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Apple Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:kCLLocationCoordinate2DInvalid addressDictionary:@{ (__bridge id)kABPersonAddressStreetKey : addr }]]; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{ MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: @(YES) }]; }]; [alertVC addAction:appleMapAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:cancelAction]; [vc presentViewController:alertVC animated:YES completion:nil]; } } @end