2013-02-23 11 views
22

GMSReverseGeocodeResponse zawieraJak uzyskać kraj, stan, miasto od reverseGeocodeCoordinate?

- (GMSReverseGeocodeResult *)firstResult; 

którego definicja jest jak:

@interface GMSReverseGeocodeResult : NSObject<NSCopying> 

/** Returns the first line of the address. */ 
- (NSString *)addressLine1; 

/** Returns the second line of the address. */ 
- (NSString *)addressLine2; 

@end 

Czy istnieje jakiś sposób, aby uzyskać ten kraj, kod ISO kraju, stan (administrative_area_1 lub odpowiadające jeden) z tych dwóch ciągów (ważne dla wszystkie kraje i wszystkie adresy)?

UWAGA: Starałem się wykonać ten fragment kodu

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse *resp, NSError *error) 
{ 
    NSLog(@"Error is %@", error) ; 
    NSLog(@"%@" , resp.firstResult.addressLine1) ; 
    NSLog(@"%@" , resp.firstResult.addressLine2) ; 
} ] ; 

ale z jakiegoś powodu nigdy obsługi została wywołana. Dodałem klucz aplikacji, a także dodałem identyfikator pakietu iOS do klucza aplikacji. W konsoli nie jest drukowany żaden błąd. Mam na myśli to, że nie jestem świadomy treści linii.

+0

Otworzyłem żądanie w mapach google iOS SDK http://code.google.com/p/gmaps-api-issues/issues/detail?id=4974 – user2101384

+0

„' GMSGeocoder' teraz udostępnia adresy strukturyzowanych poprzez 'GMSAddress ', wycofanie' GMSReverseGeocodeResult'. " - [Zestaw SDK do Mapy Google dla systemu iOS, wersja 1.7, luty 2014 r.] (Https://developers.google.com/maps/documentation/ios/releases#version_17_-_february_2014). – Pang

+0

Tak, zostało to naprawione przez Google (prawie rok później). Po prostu nie wiem, jak zamknąć to pytanie. – user2101384

Odpowiedz

33

Najprostszym sposobem jest uaktualnienie do wersji 1.7 z Google Maps SDK for iOS (wydany lutego 2014).
Z release notes:

GMSGeocoder teraz zapewnia adresy strukturyzowanych poprzez GMSAddress, deprecjację GMSReverseGeocodeResult.

Od GMSAddress Class Reference można znaleźć these properties:

coordinate
Lokalizacja lub kLocationCoordinate2DInvalid jeśli nieznany.

thoroughfare
ulica i numer domu.

locality
miejscowości lub miasta.

subLocality
Dzielnica miejscowości, dzielnicy lub park.

administrativeArea
Region/Stan/obszar administracyjny.

postalCode
pocztowy/Kod pocztowy.

country
nazwę kraju.

lines
Tablica NSString zawierających sformatowanych linii adresu.

Brak kodu kraju ISO.
Należy również pamiętać, że niektóre nieruchomości mogą zwrócić nil.

Oto pełny przykład:

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(40.4375, -3.6818) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) { 
    NSLog(@"reverse geocoding results:"); 
    for(GMSAddress* addressObj in [response results]) 
    { 
     NSLog(@"coordinate.latitude=%f", addressObj.coordinate.latitude); 
     NSLog(@"coordinate.longitude=%f", addressObj.coordinate.longitude); 
     NSLog(@"thoroughfare=%@", addressObj.thoroughfare); 
     NSLog(@"locality=%@", addressObj.locality); 
     NSLog(@"subLocality=%@", addressObj.subLocality); 
     NSLog(@"administrativeArea=%@", addressObj.administrativeArea); 
     NSLog(@"postalCode=%@", addressObj.postalCode); 
     NSLog(@"country=%@", addressObj.country); 
     NSLog(@"lines=%@", addressObj.lines); 
    } 
}]; 

a jego produkcja:

coordinate.latitude=40.437500 
coordinate.longitude=-3.681800 
thoroughfare=(null) 
locality=(null) 
subLocality=(null) 
administrativeArea=Community of Madrid 
postalCode=(null) 
country=Spain 
lines=(
    "", 
    "Community of Madrid, Spain" 
) 

Alternatywnie, można rozważyć użycie Reverse Geocoding w The Google Geocoding API (example).

15

odpowiedzi w Swift

Korzystanie z Map Google na iOS SDK (obecnie za pomocą V1.9.2 nie można określić język, w którym na powrót wyniki):

@IBAction func googleMapsiOSSDKReverseGeocoding(sender: UIButton) { 
    let aGMSGeocoder: GMSGeocoder = GMSGeocoder() 
    aGMSGeocoder.reverseGeocodeCoordinate(CLLocationCoordinate2DMake(self.latitude, self.longitude)) { 
     (let gmsReverseGeocodeResponse: GMSReverseGeocodeResponse!, let error: NSError!) -> Void in 

     let gmsAddress: GMSAddress = gmsReverseGeocodeResponse.firstResult() 
     print("\ncoordinate.latitude=\(gmsAddress.coordinate.latitude)") 
     print("coordinate.longitude=\(gmsAddress.coordinate.longitude)") 
     print("thoroughfare=\(gmsAddress.thoroughfare)") 
     print("locality=\(gmsAddress.locality)") 
     print("subLocality=\(gmsAddress.subLocality)") 
     print("administrativeArea=\(gmsAddress.administrativeArea)") 
     print("postalCode=\(gmsAddress.postalCode)") 
     print("country=\(gmsAddress.country)") 
     print("lines=\(gmsAddress.lines)") 
    } 
} 

Korzystanie z Google API V3 Odwróć geokodowanie (obecnie można specify język, w którym, aby powrócić wyników):

@IBAction func googleMapsWebServiceGeocodingAPI(sender: UIButton) { 
    self.callGoogleReverseGeocodingWebservice(self.currentUserLocation()) 
} 

// #1 - Get the current user's location (latitude, longitude). 
private func currentUserLocation() -> CLLocationCoordinate2D { 
    // returns current user's location. 
} 

// #2 - Call Google Reverse Geocoding Web Service using AFNetworking. 
private func callGoogleReverseGeocodingWebservice(let userLocation: CLLocationCoordinate2D) { 
    let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(userLocation.latitude),\(userLocation.longitude)&key=\(self.googleMapsiOSAPIKey)&language=\(self.googleReverseGeocodingWebserviceOutputLanguageCode)&result_type=country" 

    AFHTTPRequestOperationManager().GET(
     url, 
     parameters: nil, 
     success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in 
      println("GET user's country request succeeded !!!\n") 

      // The goal here was only for me to get the user's iso country code + 
      // the user's Country in english language. 
      if let responseObject: AnyObject = responseObject { 
       println("responseObject:\n\n\(responseObject)\n\n") 
       let rootDictionary = responseObject as! NSDictionary 
       if let results = rootDictionary["results"] as? NSArray { 
        if let firstResult = results[0] as? NSDictionary { 
         if let addressComponents = firstResult["address_components"] as? NSArray { 
          if let firstAddressComponent = addressComponents[0] as? NSDictionary { 
           if let longName = firstAddressComponent["long_name"] as? String { 
            println("long_name: \(longName)") 
           } 
           if let shortName = firstAddressComponent["short_name"] as? String { 
            println("short_name: \(shortName)") 
           } 
          } 
         } 
        } 
       } 
      } 
     }, 
     failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in 
      println("Error GET user's country request: \(error.localizedDescription)\n") 
      println("Error GET user's country request: \(operation.responseString)\n") 
     } 
    ) 

} 

mam nadzieję, że fragment kodu i objaśnienie pomogą przyszłym czytelnikom.

Powiązane problemy