Bound_Changed仅在最后添加的标记Google Maps v3上触发

时间:2013-11-11 02:38:09

标签: javascript jquery google-maps google-maps-api-3

我遇到了问题,因为当我更改Google地图的边界时,该事件仅适用于最后插入的标记,并且会删除所有标记详细信息列表。我想要的是当标记在谷歌地图上超出范围时,列表上该标记的详细信息也将被删除。

我没有任何错误,但我希望有人可以帮助我:)

这是我的剧本:

<script type="text/javascript">
    var geocoder;
    var map;
enter code here
    function initialize() {
        var minZoomLevel = 4;
        var zooms = 7;
        geocoder = new google.maps.Geocoder();

        map = new google.maps.Map(document.getElementById('map'), {
            zoom: minZoomLevel,
            center: new google.maps.LatLng(38.50, -90.50),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        // Bounds for North America
        var strictBounds = new google.maps.LatLngBounds(
     new google.maps.LatLng(15.70, -160.50),
     new google.maps.LatLng(68.85, -55.90)
   );

        // Listen for the dragend event
        google.maps.event.addListener(map, 'dragend', function () {
            if (strictBounds.contains(map.getCenter())) return;

            // We're out of bounds - Move the map back within the bounds

            var c = map.getCenter(),
         x = c.lng(),
         y = c.lat(),
         maxX = strictBounds.getNorthEast().lng(),
         maxY = strictBounds.getNorthEast().lat(),
         minX = strictBounds.getSouthWest().lng(),
         minY = strictBounds.getSouthWest().lat();

            if (x < minX) x = minX;
            if (x > maxX) x = maxX;
            if (y < minY) y = minY;
            if (y > maxY) y = maxY;

            map.setCenter(new google.maps.LatLng(y, x));
        });


        // Limit the zoom level
        google.maps.event.addListener(map, 'zoom_changed', function () {
            if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
        });

        codeAddress();

    }
    var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
    function codeAddress() {
        var infowindow = new google.maps.InfoWindow();
        $.getJSON('/Dashboard/LoadWorkerList', function (address) {
            $.each(address, function () {
                var currVal = this["AddressLine1"];
                geocoder.geocode({ 'address': currVal }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var marker = new google.maps.Marker({
                            map: map,
                            icon: iconBase + 'man.png',
                            position: results[0].geometry.location,
                            title: currVal


                        })



                        google.maps.event.addListener(marker, 'click', (function (marker, i) {
                            return function () {
                                infowindow.setContent(currVal);
                                infowindow.open(map, marker);
                            }
                        })(marker, currVal));
                        address.push(marker);

                        google.maps.event.addListener(map, 'bounds_changed', function () {
                            $('#places li').css('display', function () {
                               return (map.getBounds().contains($(this).data('location')))
                      ? ''
                      : 'none';
                            });
                        });
                        $('#places').append($('<li/>')
                  .text(currVal)
                   .data('location', results[0].geometry.location));
enter code here

                    }
                    else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                        setTimeout(codeAddress, 2000);
                    }
                    else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            });
        });
        google.maps.event.trigger(map, 'bounds_changed');
    }

    window.onload = function () {
        initialize();

    }
</script> 

这是我的Google地图的屏幕截图及其下面的列表:

enter image description here

1 个答案:

答案 0 :(得分:0)

所以问题在我看来是这样的。您正在地图对象上为每个标记重新定义bounds_changed事件侦听器。

相反,当边界发生变化时,你应该只有一个事件监听器,它会遍历所有标记,并决定是否显示/隐藏每个标记。

我会在创建它们时将标记粘贴到数组中,然后您可以在bounds_changed事件侦听器中轻松地循环。

相关问题