2014-09-25 56 views

回答

3

下面是我如何做它的参考,但对于苹果,我还没有找到一种方法来启动通过网址方案导航。

+ (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]]; 
    } 
} 
+0

在地图上打开地图和中心位置,但不会开始导航:( – htafoya 2018-01-20 01:22:12

2

你是对的,谷歌和苹果都需要用户输入 - 但只是按下去按钮。

如果你想同时指定的起点和终点的位置,请使用以下格式:

苹果地图:

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

谷歌地图:

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

要小心地将%20放入字符串中,因为%应该是%%或使用任何空间 – htafoya 2018-01-20 03:13:36

1

斯威夫特3的辅助类启动Apple Maps或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) 
    } 
}