2012-12-14 58 views
-3

xcode如何打開iOS 6地圖?我怎樣才能做到這一點?xcode如何打開iOS 6地圖?

感謝您的答覆enter image description here

+0

你是什麼意思?通過編程的方式? – Raptor

+1

點擊圖標上的手指?我想你需要更好的描述任務,如果你想得到任何幫助... –

回答

5

要打開與路線圖,你可以使用以下命令:

NSString *destinationAddress = @"Amsterdam"; 

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

    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    [geocoder geocodeAddressString:destinationAddress completionHandler:^(NSArray *placemarks, NSError *error) { 
     if([placemarks count] > 0) { 

      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 { 
      //error nothing found 
     } 
    }]; 
    return; 
} else { 

    NSString *sourceAddress = [LocalizedCurrentLocation currentLocationStringForCurrentLanguage]; 

    NSString *urlToOpen = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@", 
       [sourceAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
       [destinationAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]]; 
} 

這將打開地圖應用程序並檢查它是否是iOS5的或iOS6的。

對於iOS5的我用的是LocalizedCurrentLocation從這個帖子http://www.martip.net/blog/localized-current-location-string-for-iphone-apps

對於iOS6的我用CLGeocoder拿到標,然後打開它的地圖和當前位置。

請記住添加CoreLocation.framework和MapKit.framework

+0

工作!感謝你的回答 :) – Darkface35