打开Goog​​le地图应用以从其他应用程序导航

时间:2018-02-09 10:09:09

标签: ios swift xcode google-maps url-scheme

我尝试使用这些代码从我的应用中打开谷歌地图应用程序

new Date().valueOf()

我已经在我的iphone中安装了谷歌地图,但这段代码总是转到其他部分

3 个答案:

答案 0 :(得分:7)

  

启动服务(macOS中核心服务框架的一部分)   为启动应用程序和匹配文档类型提供支持   应用

您应该将此信息包含在info.plist文件

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>

如果未安装谷歌地图,您可以在浏览器中打开。

// if GoogleMap installed
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
    UIApplication.shared.openURL(NSURL(string:
        "comgooglemaps://?saddr=\(fromLatitude),\(fromLongitude)&daddr=\(toLatitude),\(toLongitude)&directionsmode=driving")! as URL)

} else {
    // if GoogleMap App is not installed
    UIApplication.shared.openURL(NSURL(string:
                "https://www.google.co.in/maps/dir/?saddr=\(fromLatitude),\(fromLongitude)&daddr=\(toLatitude),\(toLongitude)&directionsmode=driving")! as URL)
}

答案 1 :(得分:1)

在info.plist文件中添加以下内容:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps</string>
</array>

要从您的应用中打开Goog​​le地图应用,您可以使用以下方法。如果您想要导航

,将使用此方法
    + (void)routeWithGoogleMaps:(CLLocation *)destination{
        NSString *stringUrl = [NSString stringWithFormat:@"comgooglemaps://?daddr=%f,%f&directionsmode=driving", destination.coordinate.latitude, destination.coordinate.longitude];
        NSString* browserStringUrl = [NSString stringWithFormat:@"https://www.google.co.in/maps/dir/?saddr=%f,%f&directionsmode=driving", destination.coordinate.latitude, destination.coordinate.longitude];

        NSURL* url = [NSURL URLWithString:stringUrl];
        NSURL* browserUrl = [NSURL URLWithString:browserStringUrl];
        bool isGoogleMapsAppInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]];

        if(isGoogleMapsAppInstalled && [[UIApplication sharedApplication] canOpenURL:url])
            [[UIApplication sharedApplication] openURL:url];
        else
            [[UIApplication sharedApplication] openUrl:browserUrl];
    }

如果您不想浏览,但只想在该位置打开Goog​​le地图应用,请修改此网址

@"comgooglemaps://?daddr=%f,%f&directionsmode=driving"

到这个

@"comgooglemaps://?daddr=%f,%f"

对于Swift用户,您可以使用以下内容:

// if GoogleMap installed
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
    UIApplication.shared.openURL(NSURL(string:
        "comgooglemaps://?saddr=\(fromLatitude),\(fromLongitude)&daddr=\(toLatitude),\(toLongitude)&directionsmode=driving")! as URL)

} else {
    // if GoogleMap App is not installed
    UIApplication.shared.openURL(NSURL(string:
                "https://www.google.co.in/maps/dir/?saddr=\(fromLatitude),\(fromLongitude)&daddr=\(toLatitude),\(toLongitude)&directionsmode=driving")! as URL)
}

答案 2 :(得分:0)

  

如果您的应用在iOS 9.0上或之后进行了关联,则必须声明要传递给此方法的网址方案。通过在Xcode项目的Info.plist文件中使用LSApplicationQueriesSchemes数组来完成此操作。对于您希望应用程序使用此方法的每个URL方案,请将其添加为此数组中的字符串。

对于comgooglemaps://

只需添加以下字符串://。

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps</string>
</array>

希望它对你有所帮助