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

标签 iphone xcode google-maps mkmapview ios6

我想从 iOS < 6 更新一个使用 Google map 的应用。我的应用程序在 map 上有很多图钉,当用户点击其中一个图钉时,iPhone 会像共享应用程序一样调用 map ,以通过 native map 应用程序获取当前位置和目的地的方向。对于 iOS 6,相同的说明(下面发布)显然会打开 Safari 而不是 Google map 。我想编写一个 if 循环来检查设备上安装的 iOS:如果 < 6,则没有任何变化,如果 iOS > 6 那么......(打开新的苹果 map 并在那里获取方向)。

有人可以帮助我吗?

这是 iOS 6 之前的操作

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



}

最佳答案

我建议使用 [MKMapItem openMapsWithItems:],而不是在 iOS 6 中通过 URL 打开 map 应用程序。如果您使用 URL,您将无法传递“当前位置”,并且将失去进行转弯的能力 -逐向导航。 MKMapItem 有一个当前位置的特定项目,当传递该项目时,将使用当前位置作为源地址打开 map ,从而启用逐向导航。

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

关于iphone - 如何在 Xcode 中的 iPhone iOS 6 应用程序上获得从 A 到 B 的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12636707/

相关文章:

iphone - iOS 滚动界面 - FoodSpotting 注册界面

android - 从谷歌地图 Intent 获取结果

javascript - 在 jQuery 中传递变量

iphone - 传递参数更改 drawRect 方法中的统计信息

iphone - 使用 block 将NSDictionary转换为字符串?

javascript - 带有数据列表下拉菜单的 Bootstrap 输入在 iPhone (6s) 上不起作用

iphone - gzipInflate/gzipDeflate 错误

ios - '-[UITextField 日期] : unrecognized selector sent to instance 0x7f89277121c0'

ios - 使用draw(_ rect :) function时出现奇怪的黑角

javascript - 如何在 JS 中的 Google map 上的标记上方添加文本?