2012-10-17 26 views
0

如何使用Apple地圖獲取兩個地方之間的方向? 我可以提供這兩個地方的經度和緯度嗎? 是否可以在我的應用程序本身中顯示方向? 或者我是否需要在蘋果iPhone的內置Map應用中顯示方向?Apple Map:獲取方向

回答

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

下面是顯示在蘋果地圖的方向運行的代碼。它將適用於當前位置到達目的地,並且您只需要傳遞經緯度&。

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