2012-11-23 5 views
3

사용자가지도 앱 (Google 또는 Apple)을 시작하여 주소를 볼 수 있도록 허용하는 앱이 있습니다. iOS 6에서 특정 주소의 iOS지도 앱을 실행하는 방법은 무엇입니까?

내가이 수행하는 데 사용 :

Address *address = [self.person.addresses objectAtIndex:0]; 

NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@, %@ %@", 
            address.line1 == nil ? @"" : address.line1, 
            address.line2 == nil ? @"" : address.line2, 
            address.line3 == nil ? @"" : address.line3, 
            address.city == nil ? @"" : address.city, 
            address.state == nil ? @"" : address.state, 
            address.zip == nil ? @"" : address.zip]; 

NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

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

을하지만 아이폰 OS 6를 지원하기 위해, 나는이 방법으로 변경 :

Address *address = [self.person.addresses objectAtIndex:0]; 

     NSString *addressString = [NSString stringWithFormat:@"%@ %@ %@ %@, %@ %@", 
            address.line1 == nil ? @"" : address.line1, 
            address.line2 == nil ? @"" : address.line2, 
            address.line3 == nil ? @"" : address.line3, 
            address.city == nil ? @"" : address.city, 
            address.state == nil ? @"" : address.state, 
            address.zip == nil ? @"" : address.zip]; 

     // Check for iOS 6 
     Class mapItemClass = [MKMapItem class]; 
     if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
     { 
      /* 
      // Create an MKMapItem to pass to the Maps app 
      MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:nil 
                  addressDictionary:nil]; 

      MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark]; 
      [mapItem openInMapsWithLaunchOptions:nil]; 
      */ 

      // I want something like this: 
      MKMapItem *mapItem = [[MKMapItem alloc] initWithAddressQuery:addressString]; 
      [mapItem openInMapsWithLaunchOptions:nil]; 
     } 
     else 
     { 
      NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

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

     } 

기본적으로, 원시 주소 (NO ABPersonRef 년대 나 아무것도) 애플지도에서 위치를 열어야합니다. Google은이 작업을 잘 해왔습니다.

maps.google.com에서 maps.apple.com으로 간단한 전환을 시도했지만 iOS 5에서는 Google지도 웹 앱으로 열립니다. 나는 원하지 않습니다. 완벽하게 우수한 기본 앱이 있습니다.

답변

4

대답은 here입니다. 그것은 CLGeocoder 클래스를 사용하여 수행됩니다 :

// Check for iOS 6 
    Class mapItemClass = [MKMapItem class]; 
    if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) 
    { 
     CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder geocodeAddressString:addressString 
        completionHandler:^(NSArray *placemarks, NSError *error) { 

         // Convert the CLPlacemark to an MKPlacemark 
         // Note: There's no error checking for a failed geocode 
         CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0]; 
         MKPlacemark *placemark = [[MKPlacemark alloc] 
                initWithCoordinate:geocodedPlacemark.location.coordinate 
                addressDictionary:geocodedPlacemark.addressDictionary]; 

         // Create a map item for the geocoded address to pass to Maps app 
         MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark]; 
         [mapItem setName:geocodedPlacemark.name]; 

         // Set the directions mode to "Driving" 
         // Can use MKLaunchOptionsDirectionsModeWalking 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]; 

        }]; 
    } 
    //iOS 4/5: 
    else 
    { 
     NSString *mapsURL = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", [addressString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
     ... 
+0

작업도 아이폰 OS 8.4 인치 감사합니다 =) – jungledev

관련 문제