如何在我的应用 iOS6 中获取行车路线

Posted

技术标签:

【中文标题】如何在我的应用 iOS6 中获取行车路线【英文标题】:How to get driving directions within my app iOS6 【发布时间】:2013-02-11 13:12:48 【问题描述】:

我在我的应用程序中使用 Apple 地图,在我的视图中我想显示从用户位置到我在视图中的当前位置的行车方向,现在我只想要我的应用程序中的所有这些,即我可以在地图视图上显示行车方向,我尝试过使用苹果地图应用程序,但是在我从我的应用程序中调用它后,它会将我带到苹果的地图应用程序,在那里我得到行车方向,但我无法返回我的应用程序所以我我想我可以在我的应用程序本身中做一些事情,这样我就可以在我当前的视图本身上获得行车路线..

NSString* addr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=%1.6f,%1.6f",coordinate.latitude,coordinate.longitude,mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
        NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        [[UIApplication sharedApplication] openURL:url];

此代码将我从我的本地应用程序带到苹果的地图应用程序,但我无法直接返回到我的应用程序。是否有可能的解决方案,以便我可以在获得行车路线后返回我的应用程序? (webview 对我不起作用。我可以在苹果的应用程序上添加一个后退按钮还是什么)。请帮助....非常感谢!!

或者请任何人建议我一个更好的代码来实现,以便我可以在我的应用程序中完成所有这些?

我想要一个描绘导航路线和行车路线的应用内地图...

【问题讨论】:

您是指ios4(您已标记)还是iOS6(您已询问)? @Craig:抱歉,这是我在标记时出现的拼写错误.. 是的,我认为 iOS6 的可能性更大。在这种情况下,您必须小心在非 Google 地图上使用 Google 的数据。这违反了他们的 TOS,虽然我不知道他们是否或如何执行它,但最好避免激怒他们。 @Craig:是的,我会尽量避免使用它。你有任何链接吗?那么它会很有帮助..谢谢你的帮助。干杯!! 【参考方案1】:

这不是实现方向的方法,

我已经为您制作了一个示例,其中涵盖了所有 iOS 版本、New Google Maps 和 iOS 6 tom tom maps。

这里是:

if([[[UIDevice currentDevice] systemVersion] compare:@"6.0" options:NSNumericSearch] == NSOrderedDescending)
        //6.0 or above
        NSString *Destinationlatlong =[NSString stringWithFormat:@"%@,%@",your.latitude,your.longitude];

        NSString* addr = [NSString stringWithFormat:@"comgooglemaps://?saddr=%f,%f&daddr=%@",[AppDelegate zDelegate].location.coordinate.latitude,[AppDelegate zDelegate].location.coordinate.longitude, Destinationlatlong];
        addr=[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL* url = [[[NSURL alloc] initWithString:addr]autorelease];
        //    NSLog(@"url %@",url);
        if ([[UIApplication sharedApplication]canOpenURL:url]) 
            [[UIApplication sharedApplication] openURL:url];
        else
            CLLocationCoordinate2D coords =
            CLLocationCoordinate2DMake([your.latitude doubleValue],[your.longitude doubleValue]);
            MKPlacemark *placeMark = [[MKPlacemark alloc]
                                      initWithCoordinate:coords addressDictionary:nil];


            MKMapItem *destination = [[MKMapItem alloc]initWithPlacemark:placeMark];

            [destination openInMapsWithLaunchOptions:nil];
        
    else
        NSString *Destinationlatlong =[NSString stringWithFormat:@"%@,%@",your.latitude,your.longitude];
        NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=Current+Location&daddr=%@",Destinationlatlong];
        addr=[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSURL* url = [[[NSURL alloc] initWithString:addr]autorelease];
        //    NSLog(@"url %@",url);
        if ([[UIApplication sharedApplication]canOpenURL:url]) 
            [[UIApplication sharedApplication] openURL:url];
        else
            UIAlertView *alert=[[[UIAlertView alloc] initWithTitle:@"Warning!" message:@"Device does not support this functionality" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil]autorelease] ;
            [alert show];
        
    

【讨论】:

Thxs 很多...@sree charan 它为我工作了一点点变化。Thxs !!【参考方案2】:

这应该可以帮助您入门。简而言之,从 json 中的 google api 获取驾驶指令,对其进行解析,并使用 MKPolyline 将其显示在您自己的地图上 http://iosguy.com/2012/05/22/tracing-routes-with-mapkit/

【讨论】:

如果 niks2000 真的意味着 iOS6,那么在 Apple 的地图上使用谷歌的行车路线不是(合法的)选择。 我只使用苹果的地图,而不是谷歌的。如果你有任何链接,那么它会很有帮助..谢谢无论如何的帮助。干杯!!

以上是关于如何在我的应用 iOS6 中获取行车路线的主要内容,如果未能解决你的问题,请参考以下文章

在 iPhone 中获取行车路线

如何获取地图上两点之间的路线方向以绘制行车路线?

如何在我的 Rails 应用程序中获取所有路线?

Android 谷歌地图 行车路线

在颤振应用中显示行车路线

如何从地理点获取行车方向数据?