2012-03-09 14 views

回答

0

您需要做兩件事: 1.從地圖針腳獲取位置 2.打開Goog​​le地圖URL。

下面是它如何工作的(有的來自here

CLLocationCoordinate2D start = myMapView.userLocation.location.coordinate; 
CLLocationCoordinate2D destination = [pinSelected.annotation coordinate];   

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

代碼的行爲在iOS 5.x和6.x中的iOS不同假設註釋是 「toAnnotation」,下面的代碼將工作:

- (void)openMap 
{ 
    Class itemClass = [MKMapItem class]; 
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6.x and later 
     MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; 
     MKMapItem *toLocation = [[MKMapItem alloc] 
           initWithPlacemark:[[MKPlacemark alloc] 
                initWithCoordinate:CLLocationCoordinate2DMake(toAnnotation.coordinate.latitude, toAnnotation.coordinate.longitude) 
                addressDictionary:nil]]; 
     toLocation.name = toAnnotation.title; 
     [MKMapItem openMapsWithItems:[NSArray arrayWithObjects:currentLocation, toLocation, nil] 
         launchOptions:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeDriving, [NSNumber numberWithBool:YES], nil] 
                   forKeys:[NSArray arrayWithObjects:MKLaunchOptionsDirectionsModeKey, MKLaunchOptionsShowsTrafficKey, nil]]]; 
    } else { // iOS 5.1 and earlier 
     NSMutableString *mapURL = [NSMutableString stringWithString:@"http://maps.google.com/maps?"]; 
     [mapURL appendFormat:@"saddr=Current Location"]; 
     [mapURL appendFormat:@"&daddr=%f,%f", toAnnotation.coordinate.latitude, toAnnotation.coordinate.longitude]; 
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[mapURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; 
    } 
} 
0

首先您需要考慮或者Controller.h文件寫

- (IBAction)startWithOnePlacemark:(id)sender; 
- (IBAction)startWithMultiplePlacemarks:(id)sender; 
- (IBAction)startInDirectionsMode:(id)sender; 

然後在Viewcontroller.m

- (IBAction)startWithOnePlacemark:(id)sender 
{ 
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300); 
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil]; 
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark]; 
bigBenItem.name = @"Big Ben"; 

[bigBenItem openInMapsWithLaunchOptions:nil]; 

// Note: use initWithPlacemark: to initialize with CLPlacemark 
} 

- (IBAction)startWithMultiplePlacemarks:(id)sender 
{ 
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300); 
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil]; 
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark]; 
bigBenItem.name = @"Big Ben"; 

CLLocationCoordinate2D westminsterLocation = CLLocationCoordinate2DMake(51.50054300, -0.13570200); 
MKPlacemark *westminsterPlacemark = [[MKPlacemark alloc] initWithCoordinate:westminsterLocation addressDictionary:nil]; 
MKMapItem *westminsterItem = [[MKMapItem alloc] initWithPlacemark:westminsterPlacemark]; 
westminsterItem.name = @"Westminster Abbey"; 

NSArray *items = [[NSArray alloc] initWithObjects:bigBenItem, westminsterItem, nil]; 
[MKMapItem openMapsWithItems:items launchOptions:nil]; 
} 

- (IBAction)startInDirectionsMode:(id)sender 
{ 
CLLocationCoordinate2D bigBenLocation = CLLocationCoordinate2DMake(51.50065200, -0.12483300); 
MKPlacemark *bigBenPlacemark = [[MKPlacemark alloc] initWithCoordinate:bigBenLocation addressDictionary:nil]; 
MKMapItem *bigBenItem = [[MKMapItem alloc] initWithPlacemark:bigBenPlacemark]; 
bigBenItem.name = @"Big Ben"; 

CLLocationCoordinate2D westminsterLocation = CLLocationCoordinate2DMake(51.50054300, -0.13570200); 
MKPlacemark *westminsterPlacemark = [[MKPlacemark alloc] initWithCoordinate:westminsterLocation addressDictionary:nil]; 
MKMapItem *westminsterItem = [[MKMapItem alloc] initWithPlacemark:westminsterPlacemark]; 
westminsterItem.name = @"Westminster Abbey"; 

NSArray *items = [[NSArray alloc] initWithObjects:bigBenItem, westminsterItem, nil]; 
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking}; 
[MKMapItem openMapsWithItems:items launchOptions:options]; 
} 

add Mapkit,AddressBook,CoreLocation Framework

相關問題