Reputation: 8952
I have used google autocompelete api.With help of this i am able to get places in my search.After getting the address i want to get city,country,zipcode,street,longitude,latitude.I have used below code to get address.
let placesClient = GMSPlacesClient()
print("text is \(textFieldAddress.text!)")
placesClient.autocompleteQuery(textFieldAddress.text!, bounds: nil, filter:
filter){ results, error in
self.arr_addressSearch.removeAll()
if results == nil
{
return
}
for result in results!
{
print("resut is \(result.attributedFullText.string)")
print("address count is \(self.arr_addressSearch.count)")
self.arr_addressSearch.append(result.attributedFullText.string)
}
Upvotes: 2
Views: 5017
Reputation: 13329
You want to get the latitude, longitude, city, and zip code from a user-entered freeform address. Freeform means that there could be varying capitalization, punctuation, abbreviations, and spelling. Because of this, you should consider the need to parse, standardize, and verify the address.
To get the information using your code and Google's API, you need to get the placeID from a result and make a request with it: that will give you the latitude, longitude, and other information you're looking for.
I'm not sure if you've seen this, but this Address Form Autocomplete Tutorial from Google will probably help a lot.
In truth, Google isn't an address verification service. They don't focus on address standardization and verification. If verified addresses are important to you (if you want to make sure you have a real address and a real place when you search), you may want to try a different service, such as SmartyStreets. They have an iOS SDK, too. They do a great job of finding a valid address using your search result and parsing it into address components, and then returning correct place information like latitude and longitude. They match their addresses to data from postal services.
Fair disclosure: I'm an employee of SmartyStreets.
Upvotes: 0
Reputation: 13514
You can get the placeId from your code like
result.placeID
Then use the below code to get the details of the place and you will recieve GMSPlace object. From that you will find coordinate property which will hold latitude and longitude.
From the above response of result you will get place_id for the place selected and then you can use the place_id to fetch all the details for the place like below.
let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"
placesClient!.lookUpPlaceID(placeID, callback: { (place: GMSPlace?, error: NSError?) -> Void in if let error = error {
print("lookup place id query error: \(error.localizedDescription)")
return }
if let place = place {
print("Place name \(place.name)")
print("Place address \(place.formattedAddress)")
print("Place placeID \(place.placeID)")
print("Place attributions \(place.attributions)") } else {
print("No place details for \(placeID)")
}
})
For more details you can go Here.
Upvotes: 4
Reputation: 8411
From google's documentation it appears that the results of an autocompleteQuery
are an array of GMSAutocompletePredictions
objects.
As such they contain the following properties
Properties
- attributedFullText
The full description of the prediction as a NSAttributedString.
- attributedPrimaryText
The main text of a prediction as a NSAttributedString, usually the name of the place.
- attributedSecondaryText
The secondary text of a prediction as a NSAttributedString, usually the location of the place.
- placeID
An optional property representing the place ID of the prediction, suitable for use in a place details request.
- types
The types of this autocomplete result.
There doesn't appear to be away to pull the lat,long,post code etc from here as attributedFullText
, attributedPrimaryText
, attributedSecondaryText
and placeID
are just strings. (or NSAttributtedString
/NSString
to be precise)
Based on similar questions it seems what you need to do is use the placeID of each GMSAutocompletePredictions
object in a new request to get the info you need.
This should at least be a start.
Upvotes: 1