如何使用Apple地图获取两个地方之间的路线? 我能提供那两个地方的经纬度吗? 是否可以在我的应用程序中显示方向? 或者我是否需要在苹果iPhone的内置地图应用程序中显示方向?
答案 0 :(得分:2)
您可以将用户发送到iOS6中的导航应用或iOS6之前的谷歌地图。
以下是示例代码:
Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
// iOS 6 MKMapItem available
MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate:_targetLocation addressDictionary:nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:place];
destination.name = @"Name Here!";
NSArray* items = [[NSArray alloc] initWithObjects: destination, nil];
NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsDirectionsModeKey, nil];
[MKMapItem openMapsWithItems:items launchOptions:options];
} else {
// Pre-iOS 6
CLLocationCoordinate2D coords = _lastLocation.coordinate;
NSString *stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%g,%g&daddr=%g,%g", coords.latitude, coords.longitude, _targetLocation.latitude, _targetLocation.longitude];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
}
答案 1 :(得分:0)
以下是在Apple地图上显示路线的工作代码。它适用于您目的地的当前位置,您只需要通过lat& amp;目的地长。
double destinationLatitude, destinationLongitude;
destinationLatitude=// Latitude of destination place.
destinationLongitude=// Longitude of destination place.
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// Create an MKMapItem to pass to the Maps app
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(destinationLatitude,destinationLongitude);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate
addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"Name/text on destination annotation pin"];
// Set the directions mode to "Driving"
// Can use MKLaunchOptionsDirectionsModeDriving 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];