Google地方自动填充功能会移除国家/地区以进

时间:2016-11-03 17:05:03

标签: javascript ruby-on-rails google-places-api

我在我的应用中使用谷歌地方。一切正常,但我只是想从输入中删除或忽略国家/地区。

例如,如果输入“arc”,我有

Arcachon, France
Arcueil, France
Archamps, France
     

...

点击后我有query=Arcachon%2C+France

这样的查询

我只想要query=Arcachon并删除或忽略%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>

来自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

和控制器

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题:)如果有人需要帮助

解决方案只是像这样添加region=FR

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