如何使fitBounds知道自定义控件 [英] How to make fitBounds aware of custom controls

查看:82
本文介绍了如何使fitBounds知道自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的谷歌地图左侧有一个很大的(300 * 500px)自定义控件。我将我的标记集中在一起。当用户点击标记时,我想缩放地图以显示该群集中的标记。

I have a large (300*500px) custom control on the left side of my google map. I'm clustering my markers together. When a user clicks on a marker, I want to zoom the map in to show the markers in that cluster.

问题是:

当我得到我的标记集合的范围,然后 map.fitBounds(collection_bounds)时,我最终会在我的大控件下面添加标记。有没有办法阻止 fitBounds 使用整个视图端口?

When I get the bounds of my marker collection, then map.fitBounds(collection_bounds), I end up with markers underneath my large control. Is there a way to prevent fitBounds from using the whole view port?

我已经尝试获取 LatLng 我的西南边界点,将其转换为像素,将该像素移动300px,然后将返回转换为 LatLng code>作为新的西南边界点。这不起作用,因为计算是在zoom 之前完成的,所以300px的转换结果太多了......我想写我自己的 fitBounds ,但我遇到了同样的问题,因为它在缩放之前完成了。

I have tried getting the LatLng of my south west bounds point, converting that to pixels, moving that 300px in, then converting that back to a LatLng to use as the new south west bounds point. This doesn't work though because the calculations are done before the zoom, so the 300px shift ends up being too much... I thought about writing my own fitBounds, but I hit the same issue, in that it's done before the zoom.

推荐答案



What you said works:


我尝试获取我的西南边界的LatLng点,
将其转换为像素,然后移动300px,然后将
转换回LatLng以用作新的西南边界点。如果您分两步进行操作,则可以使用

I have tried getting the LatLng of my south west bounds point, converting that to pixels, moving that 300px in, then converting that back to a LatLng to use as the new south west bounds point.

,它对用户来说非常透明,因为它执行起来非常快,几乎没有注意到它。所以,首先你要做一个正常的 map.fitBounds(bounds); ,其中边界只能由你的标记定义,然后用你描述的技术重新调整。所以:

if you do it in two steps, which is pretty much transparent to the user because it is executed so fast that you hardly notice it. So, first you do a normal map.fitBounds(bounds); where bounds is only defined by your markers, and then you re-adjust with the technique you described. So:

  google.maps.event.addListenerOnce(map,'bounds_changed',function(){
    // re-adjust bounds here as you described. 
    // This event fires only once and then the handler removes itself.
  });
  map.fitBounds(bounds);

这篇关于如何使fitBounds知道自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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