4

i am able to get google places auto complete results in table view using below code. now i am looking for how to get coordinate of selected place of tableview?

 func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
    tableData.removeAll()

    for predicition in predictions
    {
         tableData.append(predicition.attributedFullText.string)
    }
    table1.reloadData()
}

Like if i select the row of table view then below method get invoked and i want to get coordinates of that particular place

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • try this https://stackoverflow.com/questions/42279252/convert-address-to-coordinates-swift – naga Sep 09 '18 at 17:30

2 Answers2

3

From the GMSAutocompletePrediction, you will get a placeID, which is a textual identifier that uniquely identifies a place. To get a place by ID, call GMSPlacesClient lookUpPlaceID:callback:, passing a place ID and a callback method.

So you need to declare another global array for saving placesID. Or you can use one array for saving a GMSAutocompletePrediction object; extract the attributedFullText in the cellForRowAtIndexPath for populating the labels and extract the placeID in didSelectRow.

Let say your tableData is an array of GMSAutocompletePrediction object.

var tableData = [GMSAutocompletePrediction]()

Then your didAutocomplete delegate method will be like this

func didAutocomplete(with predictions: [GMSAutocompletePrediction]) {
    tableData = predictions
}

And your cellForRowAtIndexPath will be :-

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    /* Intitialise cell */

    let predictionText = tableData[indexPath.row]attributedFullText.string
    print(“Populate \(predictionText) in your cell labels”)

    /* Return cell */
}

And your didSelectRowAt will be :-

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let prediction = tableData[indexPath.row]

    if let placeID = predicition.placeID {
        let placesClient = GMSPlacesClient.shared()
        placesClient.lookUpPlaceID(placeID) { (place, error) in
            if let error = error {
                print("lookup place id query error: \(error.localizedDescription)")
                return
            }

            guard let place = place else {
                print("No place details for \(placeID)")
                return
            }

            let searchedLatitude = place.coordinate.latitude
            let searchedLongitude = place.coordinate.longitude
        }
    }
}

You can explore more about it here

Vincent Joy
  • 2,598
  • 15
  • 25
  • Prediction object only returns 5 results. How can i get results more than 5? – farooq mughal Sep 09 '18 at 22:55
  • have you implemented any GMSAutocompleteFilter() in the autocompleteQuery? – Vincent Joy Sep 10 '18 at 04:58
  • this predictions object is not accessible in func didSelectRowAt – farooq mughal Sep 10 '18 at 07:23
  • its giving me error on if let placeID = prediction.placeID { . Error : Value of type 'String' has no member 'placeID' – farooq mughal Sep 10 '18 at 08:30
  • Because your tableData might still be an array of Strings. Read my answer fully. – Vincent Joy Sep 10 '18 at 09:10
  • thanks dear. its working. can you tell me how to get more than 5 results in prediction object. i did not put any filter – farooq mughal Sep 10 '18 at 10:08
  • It's a limit by Google. I found the official link explaining among other the Autocomplete Services and JSON response. "predictions contains an array of places, with information about the place. The Places API returns up to 5 results. You can read about it here “https://developers.google.com/places/web-service/autocomplete”. You need to check if you buy Enterprise Edition (Map Engine Pro) whether they are providing more than 5 results or not – Vincent Joy Sep 10 '18 at 10:22
  • can you help me in increasing results? I will give you payment for this. – farooq mughal Sep 10 '18 at 11:17
  • Its beyond my ability dear :D. In fact its beyond the limit of any developer. You have to deal with google directly for this, because as I said in my previous comment, it is the limitation set by Google for some reasons. Even if you use GMSAutocompleteViewController directly, there also only 5 results are coming. You can use this link (https://developers.google.com/places/web-service/support) for getting google support. As for the rewards part, an upvote and “mark as correct answer” would be much appreciated. – Vincent Joy Sep 10 '18 at 11:24
  • tapping the up arrow and tick mark near the top of my answer will do the trick – Vincent Joy Sep 10 '18 at 16:40
0

In the last update of Google Maps you can simply get the address

func viewController(_ viewController: GMSAutocompleteViewController, didSelect prediction: GMSAutocompletePrediction) -> Bool {
    let suggestedAddress = prediction.attributedFullText.string
    return true
}

In this delegate method you can get the full address.

Searched Address: enter image description here

Output: enter image description here

It will concatenate the upper and lower string of the searched address.