1

我遇到了问题,因为当我更改 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> 

这是我的谷歌地图的截图,其列表如下:

在此处输入图像描述

4

1 回答 1

0

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

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

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

于 2013-11-11T10:53:40.550 回答