How to get all visible markers on current zoom level

前端 未结 6 1801
情深已故
情深已故 2020-12-04 08:05

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, whic
6条回答
  •  旧巷少年郎
    2020-12-04 08:36

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

提交回复
热议问题