11

我正在构建一个应用程序,它可以打开地图应用程序,其中包含从用户当前位置到另一个位置的方向。代码如下所示:

- (id)resolveDirectionsFromCoordinate:(CLLocationCoordinate2D)startCoordinate toCoordinate:(CLLocationCoordinate2D)endCoordinate
{
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
                 startCoordinate.latitude, startCoordinate.longitude,
                 endCoordinate.latitude, endCoordinate.longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

    return nil;
}

Thos 在 iOS 5.x 中运行良好。然而,在 iOS 6 中,这会带来 Safari,因为 Maps 不再使用 Google Maps。

有谁知道我应该在 iOS 6 中调用哪个 URL?

4

4 回答 4

19

Apple 文档建议使用等效的maps.apple.com URL 方案

所以用

http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f

代替

http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f

要向后兼容,您的代码将是

    NSString* versionNum = [[UIDevice currentDevice] systemVersion];
    NSString *nativeMapScheme = @"maps.apple.com";
    if ([versionNum compare:@"6.0" options:NSNumericSearch] == NSOrderedAscending){
        nativeMapScheme = @"maps.google.com";
    }
    NSString* url = [NSString stringWithFormat: @"http://%@/maps?saddr=%f,%f&daddr=%f,%f", nativeMapScheme startCoordinate.latitude, startCoordinate.longitude,
                 endCoordinate.latitude, endCoordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 

或者,您也可以使用该maps://saddr=%f,%f&daddr=%f,%f方案,但它似乎不支持所有参数。

于 2012-09-20T10:28:50.700 回答
8

使用+ (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions方法。从MKMapItem 类参考

您可以使用此方法将一个或多个地图项传递给地图应用程序。例如,您可以使用此方法要求地图应用程序显示您的应用程序生成的基于位置的搜索结果。地图会在您指定的每个位置显示图钉,并使用每个地图项对象的内容来显示附加信息。
于 2012-09-13T14:20:51.367 回答
2
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass     respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
{
    // Create an MKMapItem to pass to the Maps app
    CLLocationCoordinate2D coordinate = 
                CLLocationCoordinate2DMake(16.775, -3.009);
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate 
                                        addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"My Place"];

    // Set the directions mode to "Driving"
    // Can use MKLaunchOptionsDirectionsModeWalking instead
    NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey :   MKLaunchOptionsDirectionsModeDriving };
    // Get the "Current User Location" MKMapItem
    MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
   // Pass the current location and destination map items to the Maps app
   // Set the direction mode in the launchOptions dictionary
   [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] 
                launchOptions:launchOptions];
   }

就这些 :)

于 2012-12-13T10:27:12.607 回答
0

I recommend checking out CMMapLauncher, a mini-library that I built to launch Apple, Google, and other iOS mapping apps with a specific mapping request. With CMMapLauncher, the code to get the directions in your question would be:

[CMMapLauncher launchMapApp:CMMapAppAppleMaps
          forDirectionsFrom:[CMMapPoint mapPointWithName:@"Origin"
                                              coordinate:startCoordinate]
                         to:[CMMapPoint mapPointWithName:@"Destination"
                                              coordinate:endCoordinate]];

As you can see, it also encapsulates the version checking required between iOS 6 & others.

于 2013-09-07T12:32:04.950 回答