2013-02-23 12 views
22

GMSReverseGeocodeResponse enthältWie man Land, Staat, Stadt von reverseGeocodeCoordinate erhält?

- (GMSReverseGeocodeResult *)firstResult; 

deren Definition ist wie:

@interface GMSReverseGeocodeResult : NSObject<NSCopying> 

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

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

@end 

Gibt es eine Möglichkeit, das Land, ISO-Ländercode, Zustand (administrative_area_1 oder entsprechenden) aus diesen zwei Saiten (gültig für zu erhalten alle Länder und alle Adressen)?

HINWEIS: Ich habe versucht, dieses Stück Code

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

Aber aus irgendeinem Grund der Handler aufgerufen wurde nie auszuführen. Ich habe den App-Schlüssel hinzugefügt und auch die iOS-Paket-ID zum App-Schlüssel hinzugefügt. In der Konsole wird kein Fehler ausgegeben. Damit meine ich den Inhalt der Zeilen nicht.

+0

Ich habe eine Anfrage in Google Maps geöffnet ios sdk http://code.google.com/p/gmaps-api-issues/issues/detail?id=4974 – user2101384

+0

"' GMSGeocoder' bietet jetzt strukturierte Adressen über 'GMSAddress ', verwarf' GMSReverseGeocodeResult'. " - [Google Maps SDK für iOS-Versionshinweise, Version 1.7, Februar 2014] (https://developers.google.com/maps/documentation/ios/releases#version_17_-_february_2014). – Pang

+0

Ja, es wurde von Google (fast 1 Jahr später) behoben. Ich weiß einfach nicht, wie ich diese Frage abschließen soll. – user2101384

Antwort

33

Der einfachste Weg ist Upgrade auf Version 1.7 der Google Maps SDK for iOS (veröffentlicht Februar 2014).
Vom release notes:

GMSGeocoder bietet nun strukturierte Adressen über GMSAddress, GMSReverseGeocodeResult ironischen.

Von GMSAddress Class Reference können Sie these properties finden:

coordinate
Ort oder kLocationCoordinate2DInvalid wenn unbekannt.

thoroughfare
Straßennummer und Name.

locality
Ort oder die Stadt.

subLocality
Vorort des Ortes, Kreis oder den Park.

administrativeArea
Region/Bundesland/Verwaltungsbereich.

postalCode
Postal/Postleitzahl.

country
Der Name Land.

lines
Ein Array von NSString enthält formatierte Linien der Adresse.

Kein ISO-Ländercode obwohl. Beachten Sie, dass einige Eigenschaften nil zurückgeben können.

Hier ist ein vollständiges Beispiel:

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

und seine Ausgabe:

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

Alternativ können Sie prüfen Reverse Geocoding im The Google Geocoding API (example) verwendet wird.

15

Antwort in Swift

Mit Google Maps iOS SDK (derzeit die V1.9.2 verwenden, können Sie nicht die Sprache festlegen, in der die Ergebnisse zurückgegeben):

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

Mit Google Reverse-Geocoding API V3 (zur Zeit können Sie die Sprache, in der specify Ergebnisse zurück):

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

} 

ich hoffe, das Code-Snippet und Erklärung wird zukünftigen Lesern helfen.

Verwandte Themen