如何获取当前缩放级别的所有可见标记
以下是一些要点:
- 我在地图上有一些标记,并且在地图旁边的右侧面板上有与其关联的记录。它们通过数字 id 连接,数字 id 存储为标记的属性。
- 所有标记都存储在一个数组中。
- 当用户放大地图时,仅与可见标记关联的记录应显示在右侧面板上。
那么,如何获取当前缩放级别上所有可见标记的列表呢?我在互联网上搜索过,但没有找到有用的东西。我想要实现的某种目标可以在此处找到
Here are some points:
- I have some markers on the map and records associated with it on the right panel besides the map. They are connected via numeric id, which is stored as a property of marker.
- All the markers are stored in an array.
- When the user zooms in the map, records, associated to only visible markers should be shown on the right panel.
So, how to get the list of all visible markers on the current zoom level? I've searched over the internet and didn't find something useful. Some kind of what I'm trying to achieve could be found here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Google Maps JavaScript API V3 中,我们可以使用如下内容:
In Google Maps JavaScript API V3 we can use something like this:
使用 GMap2.getBounds() 查找边界框。使用 GLatLngBounds.containsLatLng() 检查每个标记以查看其是否可见。
Use
GMap2.getBounds()
to find the bounding box. The useGLatLngBounds.containsLatLng()
to check each marker to see if it is visible.我知道你想要 API V2,但我必须纠正我在 @bruha 对 V3 的回复中看到的一些内容,以防有人来寻找它:
向后遍历数组,这种方式可以更快地遍历标记数组,而且我们设置了在进入循环之前绑定到变量中,因此我们不会在每次执行循环时都请求它,我们必须发出的唯一请求是特定标记是否位于边界内。
编辑:搞砸了我的减量器
编辑:map.getBounds() 应该是,是map.getBounds
I know you wanted API V2, but i had to correct some stuff i saw in @bruha's response for V3, in case someone comes looking for it:
going backwards through the array this way goes through the array of markers faster, plus we set the bounds into a variable before going into the loop so we're not requesting it every time we go through the loop, and the only request we have to make is if the specific marker lies inside the bounds.
EDIT: goofed my decrementer
EDIT: map.getBounds() should be, was map.getBounds
如果有人仍然需要这个问题的答案,我在 Codepen.io 上有一个完整的工作模型,
请随意下载并根据您的需要进行调整。
只需将 API 密钥更改为您自己的即可。 (它们是免费的)
https://codepen.io/pailwriter/pen/bGEpeRv
这里是获取视口中标记的函数。
If anyone is still needing an answer to this question, I have a complete working model on Codepen.io
Feel free to download it and tweak it for your needs.
Just please change the API key to your own. (They're free)
https://codepen.io/pailwriter/pen/bGEpeRv
Here's the function to get the markers in viewport.
这是简单的代码。试试这个代码。
It's easy code. Try this code.
我的代码片段
My code snippet