Search code examples
google-mapsgoogle-places-apiclojurescript

Clojurescript - Unable restrict to a city in Google Places Autocomplete API


I would like to search for places around a specific location. I tried it using the options location, radius, and strictbounds. But, it is always returning all the addresses in the US.

My code will look like:

(def Autocomplete (oget js/window "google.maps.places.Autocomplete"))
(defn mount-autocomplete [ctx form-props el]
  (let [autocomplete (Autocomplete. el #js {:location #js {:lat 40.730610 :lng -73.935242} :radius 15 :strictBounds true :rankBy google.maps.places.RankBy.DISTANCE :componentRestrictions #js {:country "US"}})]
    (ocall autocomplete "addListener" "place_changed"
           (fn []
            (this-as this
              (let [place (js->clj (ocall this "getPlace") :keywordize-keys true)
                    formatted-address (join-address-parts (place->address place))]
                (js/console.log formatted-address)
                (oset! el "value" (:street formatted-address))
                (doseq [[k v] formatted-address]
                  (<cmd ctx :on-change [form-props [:address k] nil v nil]))))))))

I think it takes only the componentRestrictions from the options given. Any idea?


Solution

  • For the strictBounds property to work, you need to pass a bounds object, not a location and a radius, as mentioned in the documentation.

    Set the strictBounds option to restrict the results to the given bounds, even while the viewport changes.

    https://developers.google.com/maps/documentation/javascript/places-autocomplete#set_search_area

    The bounds parameter must be a LatLngBounds object or LatLngBoundsLiteral.

    https://developers.google.com/maps/documentation/javascript/reference/places-widget#AutocompleteOptions

    If using bounds and strictBounds:true then the country component restriction isn't needed and/or could conflict with the given bounds.