在新推出的谷歌地图应用程序中显示路线

时间:2012-12-17 12:53:44

标签: google-maps

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

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

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

    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]];

2 个答案:

答案 0 :(得分:1)

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

  

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

     

if(openurlflag == 0)//打开apple maps {       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]]; }

答案 1 :(得分:0)

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

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