如何使用 Apple 地图获取两地之间的方向?我可以提供那两个地方的经纬度吗?是否可以在我的应用程序本身中显示方向?或者我是否需要在苹果 iPhone 的内置地图应用程序中显示方向?
问问题
519 次
2 回答
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];
}
于 2012-10-17T12:05:49.767 回答
0
这是在 Apple 地图上显示方向的工作代码。它适用于当前地点到您的目的地,您只需要传递目的地的纬度和经度。
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];
于 2014-02-24T14:50:30.537 回答