0

I have implemented google place autocomplete api and I have applied bounds to restrict my auto complete search to one specific city Orlando, FL, USA but Google place api is not filtering data for only this city. It is searching placing all over USA and other cities name outside USA. I want google autocomplete api to return only this city addresses. Here is the code for same:

   let placesClient = GMSPlacesClient()

   let searchBound = getCoordinateBounds(latitude: CLLocationDegrees(28.5383), longitude: CLLocationDegrees(81.3792), distance: 1.45)

    let filter = GMSAutocompleteFilter()
    filter.country = "USA"

    placesClient.autocompleteQuery(text!, bounds: searchBound, filter: filter, callback: { (results, error) in
    })

//My getCoordinateBounds method

func getCoordinateBounds(latitude:CLLocationDegrees ,
                         longitude:CLLocationDegrees,
                         distance:Double = 0.001)->GMSCoordinateBounds{
    let center = CLLocationCoordinate2D(latitude: latitude,
                                        longitude: longitude)
    let northEast = CLLocationCoordinate2D(latitude: center.latitude + distance, longitude: center.longitude + distance)
    let southWest = CLLocationCoordinate2D(latitude: center.latitude - distance, longitude: center.longitude - distance)

    return GMSCoordinateBounds(coordinate: northEast,
                               coordinate: southWest)

}

Note: I want to filter addresses those are around 100 miles radius of this latitude and longitude (that why I have given degree to 1.45). Another thing I added country filer USA after I found that my bounds filter is not working properly. So far I am able to restrict address search to USA.

Ajay Gabani
  • 1,168
  • 22
  • 38

1 Answers1

1

You should also define the boundsMode parameter in your placesClient.autocompleteQuery() call. Have a look at the documentation:

https://developers.google.com/places/ios-api/reference/interface_g_m_s_places_client

boundsMode must be equal to GMSAutocompleteBoundsMode.Restrict to
interpret bounds as a restrict. By default bounds are interpreted as bias that's why you are getting results from outside.

https://developers.google.com/places/ios-api/reference/group___autocomplete_bounds_mode#gafbfae308afa98048c1e97864baa88568

let placesClient = GMSPlacesClient()

let searchBound = getCoordinateBounds(latitude: CLLocationDegrees(28.5383), longitude: CLLocationDegrees(81.3792), distance: 1.45)

let filter = GMSAutocompleteFilter()
filter.country = "USA"

placesClient.autocompleteQuery(text!, bounds: searchBound, boundsMode: GMSAutocompleteBoundsMode.Restrict, filter: filter, callback: { (results, error) in
})

The parameter boundsMode was introduced in SDK 2.5

I hope this helps!

xomena
  • 31,125
  • 6
  • 88
  • 117