繁体   English   中英

使用Google地图和Apple Maps URL计划自动开始导航

[英]Start navigation automatically with Google Maps and Apple Maps URL Schemes

在iOS上使用URL方案启动Google地图Apple地图时,有没有办法自动启动导航?

我看到两个可选参数,但没有用户输入就没有开始导航。

这是我如何为你的参考,但对于苹果,我还没有找到一种方法来开始通过网址方案导航。

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

你是对的,谷歌和苹果都需要用户输入 - 但只能点击go按钮。

如果要同时指定起始位置和结束位置,请使用以下格式:

Apple地图:

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

谷歌地图:

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

用于启动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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM