2013-02-23 10 views
22

GMSReverseGeocodeResponse contieneCome ottenere paese, stato, città da reverseGeocodeCoordinate?

- (GMSReverseGeocodeResult *)firstResult; 

la cui definizione è come:

@interface GMSReverseGeocodeResult : NSObject<NSCopying> 

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

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

@end 

non v'è alcun modo per ottenere il Paese, Codice ISO del paese, lo stato (administrative_area_1 o corrispondente) da quei due stringhe (valido per tutti i paesi e tutti gli indirizzi)?

NOTA: ho cercato di eseguire questo pezzo di codice

[[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) ; 
} ] ; 

Ma per qualche motivo il gestore non è mai stato chiamato. Ho aggiunto la chiave dell'app e aggiunto l'ID bundle iOS alla chiave dell'app. Nessun errore è stampato nella console. Con questo intendo che non sono a conoscenza del contenuto delle linee.

+0

mi hanno aperto una richiesta in google maps SDK per iOS http://code.google.com/p/gmaps-api-issues/issues/detail?id=4974 – user2101384

+0

"' GMSGeocoder' ora fornisce indirizzi strutturati tramite 'GMSAddress ', deprecating' GMSReverseGeocodeResult'. " - [Google Maps SDK per le note di versione per iOS, versione 1.7, febbraio 2014] (https://developers.google.com/maps/documentation/ios/releases#version_17_-_febbraio_2014). – Pang

+0

Sì, è stato corretto da Google (quasi 1 anno dopo). Non so come chiudere questa domanda. – user2101384

risposta

33

Il modo più semplice è quello di l'aggiornamento alla versione 1.7 del Google Maps SDK for iOS (uscito febbraio 2014).
Dal release notes:

GMSGeocoder ora fornisce indirizzi strutturati via GMSAddress, deprecando GMSReverseGeocodeResult.

Da GMSAddress Class Reference, è possibile trovare these properties:

coordinate
posizione, o se kLocationCoordinate2DInvalid sconosciuto.

thoroughfare
Numero civico e il nome.

locality
Località o città.

subLocality
Suddivisione della località, quartiere o parco.

administrativeArea
Regione/Stato/Area amministrativa.

postalCode
postale/CAP.

country
Il nome del paese.

lines
Un array di NSString contenenti linee formattate dell'indirizzo.

Nessun codice paese ISO però.
Si noti inoltre che alcune proprietà potrebbero restituire nil.

Ecco un esempio completo:

[[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); 
    } 
}]; 

e la sua uscita:

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" 
) 

In alternativa, potete prendere in considerazione Reverse Geocoding nel The Google Geocoding API (example).

15

risposta in Swift

Uso di Google Maps per iOS SDK (attualmente utilizzando il v1.9.2 non è possibile specificare la lingua in cui restituire risultati):

@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)") 
    } 
} 

Utilizzo di Google Reverse Geocoding API V3 (attualmente è possibile specify la lingua in cui per restituire i risultati):

@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") 
     } 
    ) 

} 

Spero che questo frammenti di codice e spiegazioni aiuteranno i futuri lettori.