2012-07-05 49 views
2

我正在Xcode 4.3.3,iOS 5和Mapkit庫上開發。 該應用應該顯示Google地圖上的當前位置,獲取目的地址,並最終繪製這兩點之間的最短路徑。使用Mapkit和Google地圖的iPhone中的GPS路由(最短路徑)

我用這個教程執行應用程序,我現在有當前的位置: http://blog.objectgraph.com/index.php/2009/04/02/iphone-sdk-30-playing-with-map-kit/

我尋找路由,但我還沒有發現任何資源。 請指導我如何繪製當前位置和目的地之間的最短路徑。

謝謝!

回答

0

iOS MapKit不提供應用程序作爲服務的路由信息​​。相反,您現在的選擇是從第三方機制獲取路由信息,或者自行構建路由(如果您沒有基礎街道數據,則基本不可能)。

如果您需要在自己的應用程序中提供路由信息,或者想要利用Apple的Map進行導航,則可以考慮將地圖路由到某個位置,否則,您可能會將CloudMade作爲替代地圖系統。

如果您想調出Map App進行導航,您基本上需要打開一個包含源和目標位置的URL。這裏有一個例子:

CLLocationManager *manager = [[[CLLocationManager alloc] init] autorelease]; 
    CLLocationCoordinate2D currentLocation = [manager location].coordinate; 
    NSString *from = [NSString stringWithFormat: @"%f,%f", 
         currentLocation.latitude, currentLocation.longitude]; 
    // used to be able to (3.2 iPad) "Current+Location", now we have to send: lat,long 
    NSString *encodedAddress = [address stringByAddingPercentEscapesUsingEncoding: 
           NSUTF8StringEncoding]; 
    NSString *url = [NSString stringWithFormat: 
        @"http://maps.google.com/maps?saddr=%@&daddr=%@", from, encodedAddress]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 
4

你可以試試這個只適用於iOS 6

Class itemClass = [MKMapItem class]; 
     if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { 

      CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
      CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:getLatitude 
                   longitude:getLongitude]; 

      [geocoder reverseGeocodeLocation:newLocation 
          completionHandler:^(NSArray *placemarks, NSError *error) { 

           MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]]; 

           MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark]; 

           MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation]; 

           NSArray *mapItems = @[mapItem, mapItem2]; 

           NSDictionary *options = @{ 
          MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, 
          MKLaunchOptionsMapTypeKey: 
           [NSNumber numberWithInteger:MKMapTypeStandard], 
          MKLaunchOptionsShowsTrafficKey:@YES 
           }; 

           [MKMapItem openMapsWithItems:mapItems launchOptions:options]; 

          }   ]; 
     } 
     else 
     { 
      UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [errorAlert show]; 

     } 
    } 
0
Below ans work for all ios: 
    NSString *deviceVersion = [[UIDevice currentDevice] systemVersion]; 
    NSLog(@"My Device version is :%@ ",deviceVersion); 


    //********* For ios 6 supporting devices ********* 
    if ([deviceVersion isEqualToString:@"6.0"]) 
    { 

     Class itemClass = [MKMapItem class]; 
     if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { 

      CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
      CLLocation *newLocation = [[CLLocation alloc]initWithLatitude:getLatitude 
                   longitude:getLongitude]; 

      [geocoder reverseGeocodeLocation:newLocation 
          completionHandler:^(NSArray *placemarks, NSError *error) { 

           MKPlacemark *placeMark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]]; 

           MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placeMark]; 

           MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation]; 

           NSArray *mapItems = @[mapItem, mapItem2]; 

           NSDictionary *options = @{ 
          MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, 
          MKLaunchOptionsMapTypeKey: 
           [NSNumber numberWithInteger:MKMapTypeStandard], 
          MKLaunchOptionsShowsTrafficKey:@YES 
           }; 

           [MKMapItem openMapsWithItems:mapItems launchOptions:options]; 

          }   ]; 
     } 
     else 
     { 
      UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [errorAlert show]; 

     } 
    } 
    //********* For other ios supporting devices ********* 
    else { 

     MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
     region.center.latitude = getLatitude; 
     region.center.longitude = getLongitude; 

     MKCoordinateRegion currentRegion = { {0.0, 0.0 }, { 0.0, 0.0 } }; 
     currentRegion.center.latitude = currentLatitude; 
     currentRegion.center.longitude = currentLongitude; 

     region.span.longitudeDelta = 4.0f; 
     region.span.latitudeDelta = 4.0f; 
     currentRegion.span.longitudeDelta = 4.0f; 
     currentRegion.span.latitudeDelta = 4.0f; 

     CLLocationCoordinate2D start = { currentRegion.center.latitude, currentRegion.center.longitude }; 
     CLLocationCoordinate2D destination = { region.center.latitude, region.center.longitude }; 

     NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude]; 

     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]]; 

    } 

} 
工作