2013-02-23 158 views
22

GMSReverseGeocodeResponse包含如何從reverseGeocodeCoordinate獲取國家,州,城市?

- (GMSReverseGeocodeResult *)firstResult; 

,其定義是這樣的:

@interface GMSReverseGeocodeResult : NSObject<NSCopying> 

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

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

@end 

有來自這兩個字符串的任何方式來獲得全國,ISO國家代碼,州(administrative_area_1或相應的一個)(有效期所有國家所有地址)?

注:我試圖執行這段代碼

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

但由於某些原因,處理程序從未被調用。我確實添加了應用程序密鑰,並且還將iOS捆綁軟件ID添加到了應用程序密鑰中。控制檯中沒有打印錯誤。我的意思是我不知道這些行的內容。

+0

我已經在谷歌地圖中打開了一個請求ios sdk http://code.google.com/p/gmaps-api-issues/issues/detail?id=4974 – user2101384 2013-02-23 09:41:11

+0

「'GMSGeocoder'現在通過'GMSAddress提供結構化地址',棄用'GMSReverseGeocodeResult'。「 - [Google Maps SDK for iOS版本說明,版本1.7,2014年2月](https://developers.google.com/maps/documentation/ios/releases#version_17_-_february_2014)。 – Pang 2014-02-13 02:32:46

+0

是的,它由谷歌(近1年後)修復。我只是不知道如何解決這個問題。 – user2101384 2014-02-13 11:27:09

回答

33

最簡單的方法是將升級到Google Maps SDK for iOS(2014年2月發佈)的版本1.7
release notes

GMSGeocoder現在經由GMSAddress提供結構化的地址,棄用GMSReverseGeocodeResult

GMSAddress Class Reference,你可以找到these properties

coordinate
位置,或kLocationCoordinate2DInvalid如果未知。

thoroughfare
街道號碼和名稱。

locality
地點或城市。

subLocality
分區局部性,地區或園區。

administrativeArea
地區/國家/行政轄區。

postalCode
郵政編碼。

country
的國名。

lines
NSString包含地址的格式的線的陣列。

雖然沒有ISO國家代碼。
另請注意,有些房產可能會返回nil

這裏有一個完整的例子:

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

,其輸出:

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

或者,你可以考慮在The Google Geocoding APIexample)使用Reverse Geocoding

15

搶答斯威夫特

使用谷歌地圖iOS版SDK(目前使用v1.9.2的,你不能指定要返回結果的語言):

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

使用谷歌反向地理編碼API V3 (目前您可以在specify的語言中返回結果):

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

} 

我希望這代碼片段和解釋將有助於未來的讀者。