在我的应用程序中,我想使用谷歌地图进行导航。所以我使用谷歌URL方案来实现同样的目的。导航完成后,我需要恢复我的应用程序,因此我使用了“comgooglemaps-x-callback://”选项。 我正在使用以下代码
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps-x-callback://"]])
{
NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
NSString *string = [NSString stringWithFormat:@"comgooglemaps-x-callback://?saddr=%f,%f&daddr=%f,%f&directionsmode=driving&x-success=myapp://?resume=true&x-source=%@",sourceCoords.latitude,sourceCoords.longitude, destCoords.latitude, destCoords.longitude,appName];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string] options:@{} completionHandler:^(BOOL success)
{
if (success)
{
NSLog(@"Opened");
ApplicationDelegate.isDirectedToGoogleMaps = YES;
}
}];
}
这里“myapp”是我的应用程序的 URL 方案。 请检查我输入的 Google URL 方案是否正确。当我检查谷歌文档时,他们说“comgooglemaps-x-callback://”的参数与“comgooglemaps”的参数相同,需要两个额外的参数
[NSURL URLWithString:string]
上面的代码始终给出空值,并且不会定向到谷歌地图。
仅当我使用(“comgooglemaps-x-callback://”)回调网址时才会出现此问题。 当我使用没有回调选项的 Google URL 时,我可以启动 google map 。但就我而言,我想使用回调选项,以便我可以将用户引导回源应用程序。
最佳答案
您只能使用 comgooglemaps://
打开 Google map 。
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving", encodedString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url] options:@{} completionHandler:^(BOOL success) {
if (success) {
//do something on success
}
}];
} else {
//if google map is not installed in device then we can open goole map in web
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.google.com/maps?daddr=%@", encodedString]] options:@{} completionHandler:^(BOOL success) {
if (success) {
//do something on success
}
}];
}
不要忘记包含在 Info.plist
中,如下所示。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
关于ios - 无法从我的 native iOS 应用程序中打开 google map ,网址为 "comgooglemaps-x-callback://",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54588331/