nef*_*o_x 32 google-maps google-maps-markers
以下是一些观点:
那么,如何获取当前缩放级别上所有可见标记的列表?我通过互联网搜索并没有找到有用的东西.可以在这里找到我想要实现的某种东西
bru*_*uha 48
在Google Maps JavaScript API V3中,我们可以使用以下内容:
var markers; // your markers
var map; // your map
for (var i=0; i<markers.length; i++){
if( map.getBounds().contains(markers[i].getPosition()) ){
// code for showing your object, associated with markers[i]
}
}
Run Code Online (Sandbox Code Playgroud)
bor*_*lon 22
我知道你想要API V2,但是我必须纠正我在@brhaha对V3的回复中看到的一些内容,以防有人来找它:
var markers; // your markers
var map; // your map
for(var i = markers.length, bounds = map.getBounds(); i--;) {
if( bounds.contains(markers[i].getPosition()) ){
// code for showing your object
}
}
Run Code Online (Sandbox Code Playgroud)
通过阵列倒退这种方式经过标记更快的阵列,再加上我们再进循环,使我们不能每次我们通过循环去时,要求其设置的界限到一个变量,而我们的唯一要求如果特定标记位于边界内,则为make.
编辑:我的堕落者
编辑:map.getBounds()应该是map.getBounds
小智 4
如果有人仍然需要这个问题的答案,我在 Codepen.io 上有一个完整的工作模型
请随意下载并根据您的需要进行调整。只需将 API 密钥更改为您自己的即可。(他们是免费的)
https://codepen.io/pailwriter/pen/bGEpeRv
这是在视口中获取标记的函数。
function showVisibleMarkers() {
var bounds = map.getBounds(),
count = 0;
for (var i = 0; i < markers.length; i++) {
var marker = markers[i],
infoPanel = $('.info-' + (i+1) ); // array indexes start at zero, but not our class names :)
if(bounds.contains(marker.getPosition())===true) {
infoPanel.show();
count++;
}
else {
infoPanel.hide();
}
}
$('#infos h2 span').html(count);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45316 次 |
| 最近记录: |