谷歌地图、Ruby on Rails、带一个标记的缩放级别 [英] Google Maps, Ruby on Rails, Zoom level with one marker
问题描述
我正在通过 apneadiving/Google-Maps-for-Rails 添加谷歌地图支持(感谢很棒的 gem)
I am adding google maps support with apneadiving / Google-Maps-for-Rails (thanks awesome gem)
然而,我发现了一个小故障,这很可能是我的错.
I am finding one slight glitch, however, which very likely is my fault.
auto_zoom 在有多个标记时效果很好.但是,当只有一个标记时,它会放大到最大级别,这并不漂亮.
auto_zoom works great when there are multiple markers. However, when there is only one marker it is zoomed in to the max level which is not pretty.
"zoom" 仅在 auto_zoom 为 false 时有效,所以这不是我想要的.
"zoom" will only work when auto_zoom is false, so that's not what I want.
因此,您可以使用maxZoom",但现在用户无法手动放大超过该点,这不是我想要的.
So therefore you could use "maxZoom" but now users cannot zoom in manually beyond that point which is not what I want.
有没有办法解决这个问题?我的解释有意义吗?这是 Google Maps API 的限制吗?
Is there a way around this? Is my explanation making sense? Is this a limitation of Google Maps API?
谢谢...
推荐答案
这种行为是由于 auto_zoom
谷歌地图 API 中的内置函数造成的.
This behavior is due to the auto_zoom
built-in function in the google maps api.
一种解决方法是在 gmaps
方法中将其设置为 false
:
One work around to this is to set it to false
in the gmaps
method:
<%= gmaps({
"map_options" => { "auto_zoom" => false},
"markers" => { "data" => @json }
})
%>
然后使用 gmaps4rails_callback
来满足您的需求(确保至少有 0.7.9 版本)
And then use the gmaps4rails_callback
to fit your needs (be sure to have at least version 0.7.9)
<script type="text/javascript" charset="utf-8">
function gmaps4rails_callback() {
if (Gmaps4Rails.markers.length == 1) {
//only one marker, choose the zoom level you expect
Gmaps4Rails.map.setZoom(2);
}
else{
//more than one marker, let's auto_zoom
Gmaps4Rails.map_options.auto_zoom = true;
Gmaps4Rails.adjust_map_to_bounds();
}
}
</script>
这篇关于谷歌地图、Ruby on Rails、带一个标记的缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!