How to obtain country, state, city from reverseGeocodeCoordinate?

前端 未结 4 1284
无人及你
无人及你 2020-12-07 21:26

GMSReverseGeocodeResponse contains

- (GMSReverseGeocodeResult *)firstResult;

whose definition is like:

@interf         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 21:42

    Answer in Swift

    Using Google Maps iOS SDK (currently using the V1.9.2 you cannot specify the language in which to return results):

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

    Using Google Reverse Geocoding API V3 (currently you can specify the language in which to return results):

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

    I hope this code snippet and explanation will help future readers.

提交回复
热议问题