RAMapNavigateHandler.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // RAMapNavigateHandler.m
  3. // APEX CRM
  4. //
  5. // Created by Jack on 2018/11/29.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAMapNavigateHandler.h"
  9. #import <MapKit/MapKit.h>
  10. #import <AddressBook/AddressBook.h>
  11. @implementation RAMapNavigateHandler
  12. + (void)ra_navigate:(NSString *)addr withTitle:(NSString *)title viewController:(UIViewController *)vc {
  13. if (vc) {
  14. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  15. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
  16. UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Google Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  17. NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?q=%@&directionsmode=driving",addr]
  18. stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  19. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
  20. NSLog(@"%u",success);
  21. }];
  22. // NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?center=37.782652,-122.410126&directionsmode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  23. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  24. }];
  25. [alertVC addAction:googleMapAction];
  26. }
  27. UIAlertAction *appleMapAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Apple Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  28. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  29. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:kCLLocationCoordinate2DInvalid
  30. addressDictionary:@{
  31. (__bridge id)kABPersonAddressStreetKey : addr
  32. }]];
  33. [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{
  34. MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
  35. MKLaunchOptionsShowsTrafficKey: @(YES)
  36. }];
  37. }];
  38. [alertVC addAction:appleMapAction];
  39. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  40. }];
  41. [alertVC addAction:cancelAction];
  42. [vc presentViewController:alertVC animated:YES completion:nil];
  43. }
  44. }
  45. @end