正在打开 Google 个地图应用程序以从另一个应用程序进行导航

Opening Google map app for navigation from another Application

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

if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
        UIApplication.sharedApplication().openURL(NSURL(string:
            "comgooglemaps://?saddr=\(fromLatitude),\(fromLongitude)&daddr=\(toLatitude),\(toLongitude)&directionsmode=driving")!)
    }else{
        showAlert()
    }

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

If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by using the LSApplicationQueriesSchemes array in your Xcode project’s Info.plist file. For each URL scheme you want your app to use with this method, add it as a string in this array.

对于 comgooglemaps://

只需添加不带:// 的字符串。

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

希望对您有所帮助

Launch Services (part of the Core Services framework in macOS) provides support for launching apps and matching document types to apps.

您应该将其包含在您的 info.plist 文件中

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

如果 google 没有安装地图,您可以在浏览器中打开。

// 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)
}

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

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

要从您的应用程序打开 Google 地图应用程序,您可以使用以下方法。如果您想导航,将使用此方法

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

如果您不想导航,而只想打开 Google 地图应用程序,只需在该位置修改 url 从此

@"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)
}