【问题标题】:Prevent Onload Infinite Looping防止加载无限循环
【发布时间】:2013-12-19 09:30:17
【问题描述】:

我有一个问题。 .我如何防止这种无限循环,因为我是谷歌地图和 Jquery 的新手。

我已经阅读了很多答案,但没有一个有效

这是我的代码:

<script type="text/javascript">
    var geocoder, infoBubble;
    var map;
    //var mgr;

    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() {
        infoBubble = new InfoBubble({
            map: map,
            shadowStyle: 0,
            padding: 10,
            borderRadius: 10,
            arrowSize: 15,
            maxWidth: 300,
            borderWidth: 1,
            borderColor: '#ccc',
            arrowPosition: 50,
            arrowStyle: 0
        });
        $.getJSON('/Dashboard/LoadWorkerList', function (address) {
            $.each(address, function () {
                var currVal = this["AddressLine1"];
                var Name = this["Name"];
                var Gender = this["Gender"];
                var Bdate = this["Birthdate"];
                var ID = this["Worker_ID"];
                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
                        })

                        var link = $('<a href="#">' + currVal + '</a>')
                         .data('location', results[0].geometry.location);
                        $('#places').append($('<li>').append(link));
                        link.on('click', function (event) {
                            event.preventDefault();
                            google.maps.event.trigger(address[0], "click");
                            infoBubble.removeTab(0);
                            infoBubble.addTab(Name, "Name: " + Name + "<br> Address: " + currVal + "<br> Gender: " + Gender + "<br> Birthdate: " + Bdate + "<br><br>" + '<center><a href="/Worker/WorkerDetails?workerId=' + ID + '">View Profile</a></center>');
                            infoBubble.open(map, marker);
                        }
);

                        google.maps.event.addListener(map, 'idle', function () {
                            $('#places li a').css('display', function () {
                                return (map.getBounds().contains($(this).data('location')))
                      ? ''
                      : 'none';
                            });
                        });



                        google.maps.event.addListener(marker, 'click', (function (marker, i) {
                            return function () {
                                infoBubble.removeTab(0);
                                infoBubble.addTab(Name, "Name: " + Name + "<br> Address: " + currVal + "<br> Gender: " + Gender + "<br> Birthdate: " + Bdate + "<br><br>" + '<center><a href="/Worker/WorkerDetails?workerId=' + ID + '">View Profile</a></center>');
                                infoBubble.open(map, marker);
                            }
                        })(marker, currVal));
                        address.push(marker);

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

我认为这是导致项目循环的原因

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

还有这个

` google.maps.event.addListener(map, 'zoom_changed', function () {
                if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
            });
            codeAddress();    <------- This one`

【问题讨论】:

    标签: javascript jquery asp.net-mvc-3 google-maps razor


    【解决方案1】:

    initialize 的调用不是无限循环的原因,initialize 会被调用 1 次。

    我看到无限循环的唯一原因是代码地址:

     else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                        setTimeout(codeAddress, 2000);
                    }
    

    当这个条件成立时,你再次调用codeAddress没有什么区别,你会一次又一次地得到相同的结果(延迟2秒)。

    您可以:

    1. /Dashboard/LoadWorkerList 最多返回 10 个地址
    2. /Dashboard/LoadWorkerList的返回中省略先前请求中已经返回的地址

    或:

    geocoder.geocode 的每次调用使用增加的超时时间(增加 150 毫秒)

    【讨论】:

    • Dr.Molle 是对的:在 json 文件中有 2 个条目,我得到的结果没有任何问题。将它增加到 12 我得到 OVER_QUERY_LIMIT 错误。
    • 先生。实际上,可能显示的标记数约为 10000 个地址。 .我怎么能处理它,因为它使我的地图崩溃。 ..另一个先生。 .我应该如何省略LoadWorkerList的返回
    • 先生,我有一个问题。 .为什么我的控制器已经被 JsonResult 访问了,即使我只在登录屏幕上??
    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    相关资源
    最近更新 更多