How to geocode address by google maps iOS API?

25,674

Solution 1

As others have pointed out, there is not a predefined method to do the search, but you can use network request to access the Google Geocoding API yourself:

func performGoogleSearch(for string: String) {
    strings = nil
    tableView.reloadData()

    var components = URLComponents(string: "https://maps.googleapis.com/maps/api/geocode/json")!
    let key = URLQueryItem(name: "key", value: "...") // use your key
    let address = URLQueryItem(name: "address", value: string)
    components.queryItems = [key, address]

    let task = URLSession.shared.dataTask(with: components.url!) { data, response, error in
        guard let data = data, let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, error == nil else {
            print(String(describing: response))
            print(String(describing: error))
            return
        }

        guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {
            print("not JSON format expected")
            print(String(data: data, encoding: .utf8) ?? "Not string?!?")
            return
        }

        guard let results = json["results"] as? [[String: Any]],
            let status = json["status"] as? String,
            status == "OK" else {
                print("no results")
                print(String(describing: json))
                return
        }

        DispatchQueue.main.async {
            // now do something with the results, e.g. grab `formatted_address`:
            let strings = results.compactMap { $0["formatted_address"] as? String }
            ...
        }
    }

    task.resume()
}

Solution 2

No, there is no native way in the Google Maps SDK for iOS.

This is a very popular feature request though, see: Issue 5170: Feature request: Forward geocoding (from address to coordinates)

Solution 3

Unfortunately, there is no way to do that as native. I hope that function will help.

    func getAddress(address:String){

    let key : String = "YOUR_GOOGLE_API_KEY"
    let postParameters:[String: Any] = [ "address": address,"key":key]
    let url : String = "https://maps.googleapis.com/maps/api/geocode/json"

    Alamofire.request(url, method: .get, parameters: postParameters, encoding: URLEncoding.default, headers: nil).responseJSON {  response in

        if let receivedResults = response.result.value
        {
            let resultParams = JSON(receivedResults)
            print(resultParams) // RESULT JSON
            print(resultParams["status"]) // OK, ERROR
            print(resultParams["results"][0]["geometry"]["location"]["lat"].doubleValue) // approximately latitude
            print(resultParams["results"][0]["geometry"]["location"]["lng"].doubleValue) // approximately longitude
        }
    }
}

Solution 4

Alamofire and Google's Geodecode API

Swift 4

func getAddressFromLatLong(latitude: Double, longitude : Double) {
    let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=YOUR_API_KEY_HERE"

    Alamofire.request(url).validate().responseJSON { response in
        switch response.result {
        case .success:

            let responseJson = response.result.value! as! NSDictionary

            if let results = responseJson.object(forKey: "results")! as? [NSDictionary] {
                if results.count > 0 {
                    if let addressComponents = results[0]["address_components"]! as? [NSDictionary] {
                        self.address = results[0]["formatted_address"] as? String
                        for component in addressComponents {
                            if let temp = component.object(forKey: "types") as? [String] {
                                if (temp[0] == "postal_code") {
                                    self.pincode = component["long_name"] as? String
                                }
                                if (temp[0] == "locality") {
                                    self.city = component["long_name"] as? String
                                }
                                if (temp[0] == "administrative_area_level_1") {
                                    self.state = component["long_name"] as? String
                                }
                                if (temp[0] == "country") {
                                    self.country = component["long_name"] as? String
                                }
                            }
                        }
                    }
                }
            }
        case .failure(let error):
            print(error)
        }
    }
}

Solution 5

If you are just looking for a Geocoding solution you could look into a little open source project I built. It is very lightweight and uses OpenStreetMap's geocoding API called Nominatim. Check it out here: https://github.com/caloon/NominatimSwift

You can even search for landmarks.

Geocoding addresses and landmarks:

Nominatim.getLocation(fromAddress: "The Royal Palace of Stockholm", completion: {(error, location) -> Void in
  print("Geolocation of the Royal Palace of Stockholm:")
  print("lat = " + (location?.latitude)! + "   lon = " + (location?.longitude)!)
})
Share:
25,674
Jurasic
Author by

Jurasic

Updated on January 19, 2020

Comments

  • Jurasic
    Jurasic over 4 years

    I found one way to send request:

    A Google Maps Geocoding API request takes the following form:

    https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters where outputFormat may be either of the following values:

    json (recommended) indicates output in JavaScript Object Notation (JSON); or xml indicates output in XML To access the Google Maps Geocoding API over HTTP, use:

    But it's really inconvenient, is there any native way in swift?

    I looked into GMSGeocoder interface and only reverse geocoding can be done by it's API.

  • Isuru
    Isuru almost 4 years
    Strangely I get the error This IP, site or mobile application is not authorized to use this API key. Request received from IP address xxx.xx.xx.xx, with empty referer even though I have enabled the API and restricted it with my app's bundle identifier.
  • Isuru
    Isuru almost 4 years
    Update: Found the fix. Apparently I had to pass the bundle identifier as a header value X-Ios-Bundle-Identifier in this request. That's when it worked.