如何获取当前缩放级别的所有可见标记

发布于 2024-09-03 13:35:45 字数 304 浏览 7 评论 0原文

以下是一些要点:

  1. 我在地图上有一些标记,并且在地图旁边的右侧面板上有与其关联的记录。它们通过数字 id 连接,数字 id 存储为标记的属性。
  2. 所有标记都存储在一个数组中。
  3. 当用户放大地图时,仅与可见标记关联的记录应显示在右侧面板上。

那么,如何获取当前缩放级别上所有可见标记的列表呢?我在互联网上搜索过,但没有找到有用的东西。我想要实现的某种目标可以在此处找到

Here are some points:

  1. 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.
  2. All the markers are stored in an array.
  3. 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 技术交流群。

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

发布评论

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

评论(6

何处潇湘 2024-09-10 13:35:45

在 Google Maps JavaScript API V3 中,我们可以使用如下内容:

let markers
let map
const bounds = map.getBounds()
markers.filter(m => m.isAdded).forEach(m => {
  if (bounds.contains(m.getPosition())) {
    // code for showing your object, associated with current marker
  }
})

In Google Maps JavaScript API V3 we can use something like this:

let markers
let map
const bounds = map.getBounds()
markers.filter(m => m.isAdded).forEach(m => {
  if (bounds.contains(m.getPosition())) {
    // code for showing your object, associated with current marker
  }
})
送你一个梦 2024-09-10 13:35:45

使用 GMap2.getBounds() 查找边界框。使用 GLatLngBounds.containsLatLng() 检查每个标记以查看其是否可见。

Use GMap2.getBounds() to find the bounding box. The use GLatLngBounds.containsLatLng() to check each marker to see if it is visible.

太傻旳人生 2024-09-10 13:35:45

我知道你想要 API V2,但我必须纠正我在 @bruha 对 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
    }
}

向后遍历数组,这种方式可以更快地遍历标记数组,而且我们设置了在进入循环之前绑定到变量中,因此我们不会在每次执行循环时都请求它,我们必须发出的唯一请求是特定标记是否位于边界内。

编辑:搞砸了我的减量器

编辑: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:

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

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

柠北森屋 2024-09-10 13:35:45

如果有人仍然需要这个问题的答案,我在 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);
}

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.

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);
}
魂牵梦绕锁你心扉 2024-09-10 13:35:45

这是简单的代码。试试这个代码。

private boolean CheckVisibility(Marker marker)
{
    if(googleMap != null)
    {
        //This is the current user-viewable region of the map
        LatLngBounds latLongBounds = googleMap.getProjection().getVisibleRegion().latLngBounds;

            if(latLongBounds.contains(marker.getPosition()))
                   //If the item is within the the bounds of the screen
                  return true;
            else
                  //If the marker is off screen
                  return false;
    }
    return false;
}

It's easy code. Try this code.

private boolean CheckVisibility(Marker marker)
{
    if(googleMap != null)
    {
        //This is the current user-viewable region of the map
        LatLngBounds latLongBounds = googleMap.getProjection().getVisibleRegion().latLngBounds;

            if(latLongBounds.contains(marker.getPosition()))
                   //If the item is within the the bounds of the screen
                  return true;
            else
                  //If the marker is off screen
                  return false;
    }
    return false;
}
爱要勇敢去追 2024-09-10 13:35:45

我的代码片段

private boolean isAnyMarkerVisible(LatLng ll) {
    if(gMap != null && markersData != null) {
        final LatLngBounds latLongBounds = LatLngBounds.builder().include(ll).build();
        for (Store store : markersData) {
            if (latLongBounds.contains(store.getLatLng())) {
                return true;
            }
        }
    }
    return false;
}

My code snippet

private boolean isAnyMarkerVisible(LatLng ll) {
    if(gMap != null && markersData != null) {
        final LatLngBounds latLongBounds = LatLngBounds.builder().include(ll).build();
        for (Store store : markersData) {
            if (latLongBounds.contains(store.getLatLng())) {
                return true;
            }
        }
    }
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文