如何实现Gmaps4rails和自动完成

时间:2016-10-23 14:35:21

标签: google-maps autocomplete gmaps4rails

我真的很失望,我试图在我的应用程序上实现自动完成,但我不知道怎么做。我在谷歌,StackOverflow上搜索过但我找不到要理解的东西......我想只为法国自动完成。

所以我尝试了这个,但是这段代码不起作用

我的观点(脚本和表格)

<%= 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>
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();

});
 $(document).ready(initialize_gmaps_autocomplete_user_address());

function initialize_gmaps_autocomplete_user_address() {
var input = document.getElementById('query');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.setTypes(['geocode']);
}
</script>

控制器

  @hash = Gmaps4rails.build_markers(@campings) do |camping, marker|
        marker.lat camping.latitude
        marker.lng camping.longitude
        marker.infowindow render_to_string(:partial => "/campings/infowindow", :locals => { :camping => camping})
        marker.picture ({
          "url" => "http://avantjetaisriche.com/map-pin.png",
          "width" =>  29,
          "height" => 32})

我的application.js

/= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require underscore
//= require gmaps/google
//= validyoutube
//= require_tree .
你能帮帮我吗?感谢。

1 个答案:

答案 0 :(得分:0)

为了帮助您开始实施,您可以尝试浏览这些有用的GitHub帖子:

此外,请尝试浏览Place Autocomplete,其中提到地方自动填充请求是以下内容的HTTP网址:

https://maps.googleapis.com/maps/api/place/autocomplete/output?parameters

并且,要将其设置为您的首选语言,请在您的请求中添加language参数,其值等于您选择的语言代码。请参阅list of supported languages及其代码。

您还可以检查gmaps4rails with Google Maps searchbox中给出的解决方案,看看它是否有帮助。