如何在Xcode中从A到B获取iPhone iOS 6应用程序的方向?

The*_*One 6 iphone xcode google-maps mkmapview ios6

我想更新iOS <6中使用Google地图的应用.我的应用程序在地图上有很多针脚,当用户点击其中一个时,iPhone会像分享应用程序一样调用地图,以便从当前位置和目标地图应用程序获取方向.对于iOS 6,相同的说明(下面发布)显然打开Safari而不是谷歌地图.我想写一个if-cycle来检查设备上安装的iOS:如果<6,没有任何改变,如果iOS> 6则.....(打开新的苹果地图并获得方向).

有人可以帮帮我吗?

这里是iOS 6之前的Action

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {

    [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];


    NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];

    NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
    [[UIApplication sharedApplication] openURL:url];



}
Run Code Online (Sandbox Code Playgroud)

bro*_*nko 14

我建议使用[MKMapItem openMapsWithItems:],而不是通过iOS 6中的URL打开地图应用程序.如果您使用URL,您将无法通过"当前位置",并且将失去转弯的能力导航.MKMapItem具有当前位置的特定项目,当传递时,将使用当前位置作为源地址打开地图,从而实现逐向导航.

- (void)openMapsWithDirectionsTo:(CLLocationCoordinate2D)to {
    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil] autorelease]];
        toLocation.name = @"Destination";
        [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil]
                       launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil]
                                                                 forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]];
        [toLocation release];
    } else {
        NSMutableString *mapURL = [NSMutableString stringWithString:@"http://maps.google.com/maps?"];
        [mapURL appendFormat:@"saddr=Current Location"];
        [mapURL appendFormat:@"&daddr=%f,%f", to.latitude, to.longitude];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[mapURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    }
}
Run Code Online (Sandbox Code Playgroud)


The*_*One 4

我使用了如下所示的预处理器代码来定义 if 循环的条件。

#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
Run Code Online (Sandbox Code Playgroud)

然后:

if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
         NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
         NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
         [[UIApplication sharedApplication] openURL:url];
    }

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


    }
Run Code Online (Sandbox Code Playgroud)

看来可行再见!