Google 地图、Ruby on Rails、使用一个标记的缩放级别

发布于 2024-11-03 18:25:45 字数 329 浏览 1 评论 0原文

我正在通过 apneadiving / Google-Maps-for-Rails 添加谷歌地图支持(感谢很棒的 gem),

但是我发现了一个小故障,这很可能是我的错。

当有多个标记时,auto_zoom 效果很好。然而,当只有一个标记时,它会放大到最大级别,这不太漂亮。

“zoom”仅在 auto_zoom 为 false 时才起作用,所以这不是我想要的。

因此,您可以使用“maxZoom”,但现在用户无法手动放大到超出该点,这不是我想要的。

有办法解决这个问题吗?我的解释有道理吗?这是 Google 地图 API 的限制吗?

谢谢...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北陌 2024-11-10 18:25:45

此行为是由于 google 地图 API 中的 auto_zoom 内置函数造成的。

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

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

然后使用 gmaps4rails_callback 来满足您的需求(请务必版本至少为 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>

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>
路还长,别太狂 2024-11-10 18:25:45

我以一种稍微不同的方式实现了这一点,因为我知道我的地图上只会有一个标记。我对 Rails 比较陌生,但在你看来,这种方法比使用 JS 感觉更“干净”一些。

我的模型中存储了 lat 和 lng(在创建时由 geokit 编码),因此在我看来执行了以下操作:

<%= gmaps({
       "map_options" => {"auto_zoom" => false, "zoom" => 15, "center_latitude" => @listing.lat, "center_longitude" => @listing.lng },
       "markers"     => {"data" => @markers }
      })
%>

@markers 是我由 blah.to_gmaps4rails 创建的 JSON,“listing”是我的模型。

I achieved this in a slightly different way as I know that I'll only ever have one marker on my map. I'm relatively new to rails, but this method feels a bit "cleaner" than using JS in your view.

I've got lat and lng stored in my model (encoded by geokit at time of creation), so did the following in my view:

<%= gmaps({
       "map_options" => {"auto_zoom" => false, "zoom" => 15, "center_latitude" => @listing.lat, "center_longitude" => @listing.lng },
       "markers"     => {"data" => @markers }
      })
%>

@markers is my JSON created by blah.to_gmaps4rails, and "listing" is my model.

凌乱心跳 2024-11-10 18:25:45

谢谢这对我有帮助...

{"auto_zoom" => false, "zoom" => 15, "center_latitude" => @listing.lat, "center_longitude" => @listing.lng },
"markers" => {"data" => @markers }
})
%>

thanks this helped me...

{"auto_zoom" => false, "zoom" => 15, "center_latitude" => @listing.lat, "center_longitude" => @listing.lng },
"markers" => {"data" => @markers }
})
%>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文