2

我在 gmaps4rails gem 和 google place API 之间存在兼容性问题

基本上,如果我将 gmpas4rails 放在我的视图中,如下所示:

<%=gmaps%>

然后这个脚本不再工作(它通常会在 user_address 字段上激活自动完成功能:

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

function initialize_gmaps_autocomplete_user_address() {
var input = document.getElementById('user_address');
var defaultBounds = new google.maps.LatLngBounds(new google.maps.LatLng(42.71422,-4.222666), new google.maps.LatLng(51.179343,8.47412));
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.setBounds(defaultBounds);
autocomplete.setTypes(['geocode']);
}

我试图通过 gmaps4rails.call_back 调用脚本并更改脚本中的 var 名称......

任何的想法?

4

2 回答 2

4

我猜你第二次调用谷歌 api 把事情搞砸了。

这里有两个解决方案:

1)不包括部分的js:

<%= gmaps({your_hash}, true, false) %>

并手动包含所有 js 文件,但一定要写:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&amp;libraries=geometry,places"></script>

这是原始部分,因此您不会忘记任何文件。

2)在git上抓取当前版本,places直接添加:

<%= gmaps({ "map_options" => { "libraries" => ["places"] } }) %>

我稍后会更新 gem,所以上面的版本0.10.2会包含这个。

于 2011-08-06T19:03:26.673 回答
1

实际上2)应该是

<%= gmaps({:map_options => {:libraries => ["places"]}}) %>

因为在 gmaps4rails 的源代码中使用符号而不是字符串。

要使其与字符串一起使用,您必须使用 HashWithIndifferentAccess:

<%= gmaps(HashWithIndifferentAccess.new("map_options" => {"libraries" => ['places']}))%>
于 2011-10-25T13:20:24.870 回答