Start navigation automatically using Google Maps and Apple Maps URL schemes

Is there a way to start navigation automatically when starting Google Maps or Apple Maps with a URL scheme on iOS?

I see several optional parameters for both, but not for starting navigation without user input.

+6
source share
3 answers

Here's how I did it for your reference, but for the apple, I did not find a way to start navigating the URL scheme.

+ (void)navigateToLocation:(CLLocation*)_navLocation { if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]]) { NSString *string = [NSString stringWithFormat:@"comgooglemaps://?daddr=%f,%f&directionsmode=driving",_navLocation.coordinate.latitude,_navLocation.coordinate.longitude]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]]; } else { NSString *string = [NSString stringWithFormat:@"http://maps.apple.com/?ll=%f,%f",_navLocation.coordinate.latitude,_navLocation.coordinate.longitude]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]]; } } 
+3
source

You are correct, Google and Apple require user input, but only in order to press the go button.

If you want to specify a start and end location, use the following format:

Apple Maps:

 http://maps.apple.com/maps?saddr=Current%20Location&daddr=<Your Location> 

Google Maps:

 comgooglemaps-x-callback://?saddr=&daddr=<Your Location> 
+2
source

Swift 3 helper classes to run Apple Maps or Google Maps

  struct LinksHelper { static func startNavigation(coordinate: CLLocationCoordinate2D) { struct Links { static let kGoogleMapsSchema = "comgooglemaps://" static let kGoogleMaps = "\(kGoogleMapsSchema)?daddr=%f,%f&directionsmode=driving" static let kAppleMaps = "https://maps.apple.com/?saddr=Current Location&daddr=%f,%f&z=10&t=s" } var path: String! if let googleMapsSchemaUrl = URL(string:Links.kGoogleMapsSchema), UIApplication.shared.canOpenURL(googleMapsSchemaUrl) { path = Links.kGoogleMaps } else { path = Links.kAppleMaps } guard let str = String(format: path, coordinate.latitude, coordinate.longitude).addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed) else { return } guard let url = URL(string: str) else { return } UIApplication.shared.openURL(url) } } 
+1
source

Source: https://habr.com/ru/post/975833/


All Articles