frontcodelover
frontcodelover

Reputation: 337

Google place autocomplete remove country to query

I m using google places in my app. All works fine, but i just want to remove or ignore country from input.

For example if it type "arc" i have

Arcachon, France
Arcueil, France
Archamps, France

...

After clicking I have query like this query=Arcachon%2C+France

I want only this query=Arcachon and remove or ignore %2C+France

My js in home.html.erb

    <%= form_tag(result_path, method: :get) %>
    <%= text_field_tag :query, params[:query], class:"search-query form-control", placeholder:"Ex: Corse, Arcachon..."  %>
    <%= submit_tag "Partez", class:"btn btn-danger", name: nil %>


<script>
  function init() {
    var options = {
        types: ['(cities)'],
        componentRestrictions: {country: "fr"}};
    var input = document.getElementById('query');
    var autocomplete = new google.maps.places.Autocomplete(input, options);}

    google.maps.event.addDomListener(window, 'load', init);
</script>

my query from model.rb

def self.search(query)
     return scoped unless query.present?
     where(['nomdep LIKE ? OR name LIKE ? OR nomregion LIKE ? OR commune LIKE?', "%#{query.mb_chars.downcase}%", "%#{query.mb_chars.downcase}%", "%#{query.mb_chars.downcase}%", "%#{query.mb_chars.downcase}%"])
  end

and controller

def result
      if params[:query].blank?
        redirect_to action: :index and return
      else
        @campings = Camping.search(params[:query])
  end
end

Thanks for your help.

Upvotes: 0

Views: 1398

Answers (2)

Ghayour Ahmad
Ghayour Ahmad

Reputation: 1

var place = autocomplete.getPlace();

                    var country = "";
                    for (var i = 0; i < place.address_components.length; i++) {
                        if (place.address_components[i].types.includes("country")) {
                            country = place.address_components[i].long_name;
                            break;
                        }
                    }
                    country = country.trim();
                    // alert(country);
                    if (country !== '') {
                        alert(country);
                        var inputValue = input.value;
                        var lastIndex = inputValue.lastIndexOf(country);


            if (lastIndex !== -1) {
                var commaIndex = inputValue.lastIndexOf(',', lastIndex);
                if (commaIndex !== -1) {
                    input.value = inputValue.substring(0, commaIndex) + inputValue.substring(lastIndex + country.length);
                }
            }

                        // alert(input.value);
                    }
            if (country !== '') {
                // alert(country);
                input.value = input.value.replace(country , '');
                alert(input.value);
            }

Upvotes: 0

frontcodelover
frontcodelover

Reputation: 337

I solved this question :) If someone need help

Solution is only add region=FR like this

 <script src="https://maps.google.com/maps/api/js?v=3.23&key=MYKEY=places&region=FR"></script>

Upvotes: 2

Related Questions