0

我正在寻找将用户从我们的应用程序重定向到谷歌地图应用程序以显示两个位置之间的路线的方法。目前苹果地图仅支持美国的路线,但我想为其他地区发布我的应用程序。

在苹果为 ios 推出谷歌地图应用程序前几天,我想在其中重定向我的用户。

现在我正在使用以下代码在苹果地图中重定向用户:

    NSString * myLocString = [NSString stringWithFormat:@"40.737893,-73.990173"];

    NSString * destString = [NSString stringWithFormat:@"40.733893,-73.993173"];
NSString* queryString = [NSString stringWithFormat:@"saddr=%@&daddr=%@",myLocString,destString];



NSString* searchQuery = nil;

searchQuery =  [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString* urlString = [NSString stringWithFormat:@"http://maps.apple.com/maps?%@",searchQuery];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
4

2 回答 2

1

实现这一目标的简单方法:

BOOL openurlflag=[[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];

if (openurlflag==0)// 打开苹果地图 { NSString * myLocString = [NSString stringWithFormat:@"40.737893,-73.990173"];

NSString * destString = [NSString stringWithFormat:@"40.733893,-73.993173"];

NSString* queryString = [NSString stringWithFormat:@"saddr=%@&daddr=%@",myLocString,destString];

NSString* searchQuery = nil;

searchQuery =  [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString* urlString = [NSString stringWithFormat:@"http://maps.apple.com/maps?%@",searchQuery];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
 } else if (openurlflag==1) // open google maps {
NSString * myLocString = [NSString stringWithFormat:@"40.737893,-73.990173"];

NSString * destString = [NSString stringWithFormat:@"40.733893,-73.993173"];

        NSString* queryString = [NSString stringWithFormat:@"saddr=%@&daddr=%@&directionsmode=transit",myLocString,destString];


NSString* searchQuery = nil;

searchQuery =  [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString* urlString = [NSString stringWithFormat:@"comgooglemaps://?%@",searchQuery];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; }
于 2012-12-17T13:53:00.747 回答
0

最后我得到了一个链接,您可以通过更改 URLScheme 来实现。

https://developers.google.com/maps/documentation/ios/urlscheme

于 2012-12-17T12:57:16.450 回答