Google地图,Ruby on Rails,缩放级别和一个标记 [英] Google Maps, Ruby on Rails, Zoom level with one marker

查看:98
本文介绍了Google地图,Ruby on Rails,缩放级别和一个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在添加Google地图支持与apneadiving / Google-Maps-for-Rails(感谢真棒宝石)



然而,我发现了一个小故障可能是我的错。

当有多个标记时,auto_zoom效果很好。但是,当只有一个标记时,它会放大到最大程度,这不是很漂亮。



zoom只有在auto_zoom为false时才有效,所以这不是我想要的。因此,您可以使用maxZoom,但现在用户无法在超出该点的位置进行手动放大,这不是我想要的。



有没有办法解决这个问题?我的解释是否合理?这是Google Maps API的限制吗?



谢谢...

解决方案

这种行为是由于 auto_zoom google maps api中的内置函数。



解决此问题的方法是将其设置为 false 在 gmaps 方法中:

 <%= gmaps({
map_options=> {auto_zoom=> false},
markers=> {data=& json}
})
%>

然后使用 gmaps4rails_callback 来适合您的(确保至少有0.7.9版本)

 < script type =text / javascriptcharset = UTF-8\" > 
函数gmaps4rails_callback(){
if(Gmaps4Rails.markers.length == 1){
//只有一个标记,选择您期望的缩放级别
Gmaps4Rails.map.setZoom (2);
}
else {
//多于一个标记,让我们auto_zoom
Gmaps4Rails.map_options.auto_zoom = true;
Gmaps4Rails.adjust_map_to_bounds();
}
}
< / script>


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 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" will only work when auto_zoom is false, so that's not what I want.

So therefore you could use "maxZoom" but now users cannot zoom in manually beyond that point which is not what I want.

Is there a way around this? Is my explanation making sense? Is this a limitation of Google Maps API?

Thanks...

解决方案

This behavior is due to the auto_zoom built-in function in the google maps api.

One work around to this is to set it to false in the gmaps method:

<%= gmaps({
       "map_options" => { "auto_zoom" => false},
       "markers"     => { "data" => @json }
      })
%>

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>

这篇关于Google地图,Ruby on Rails,缩放级别和一个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆