在iOS上使用URL方案启动Google Maps或Apple Maps时,是否有办法自动启动导航?
我看到两个可选参数,但没有用户输入就没有开始导航。
答案 0 :(得分:4)
以下是我的参考方式,但是对于苹果,我还没有办法通过网址方案开始导航。
+ (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]];
}
}
答案 1 :(得分:2)
你是对的,谷歌和苹果都需要用户输入 - 但只需点击“开始”按钮。
如果要同时指定开始和结束位置,请使用以下格式:
Apple Maps:
http://maps.apple.com/maps?saddr=Current%20Location&daddr=<Your Location>
Google地图:
comgooglemaps-x-callback://?saddr=&daddr=<Your Location>
答案 2 :(得分:2)
用于启动Apple Maps或Google Maps导航的Swift 3帮助程序类
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)
}
}