2012-03-09 7 views

答えて

0

2つの操作が必要です: 1.地図ピンから位置を取得します 2. Googleマップを開きます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とのiOS 6.xの上の異なる振る舞いをどのように動作するかです注釈は "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ファイル内に必要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]; 
} 

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

を書きます

Mapkit、AddressBook、CoreLocation Frameworkを追加する

関連する問題