简体   繁体   English

在 jquery.each() 之外调用 fitbounds 的问题

[英]Problems calling fitbounds outside jquery.each()

I'm working on a Google Map in JavaScript(v3).我正在用 JavaScript(v3) 开发谷歌地图。 I need to show some markers from XML, for which I use jQuery.我需要从 XML 中显示一些标记,为此我使用了 jQuery。

Here's the object and function, might save me time explaining:这是对象和函数,可能会节省我解释的时间:

    var VX = {
    map:null,
    bounds:null
}
VX.placeMarkers = function(filename) {
    $.get(filename, function(xml) {
        $(xml).find("marker").each(function() {
            var lat         = $(this).find('lat').text();
            var lng         = $(this).find('lng').text();
            var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
            VX.bounds.extend(point);                
            VX.map.fitBounds(VX.bounds);    //this works        
            var marker = new google.maps.Marker({
                position: point,
                map: VX.map,
                zoom: 10,
                center: point
                });
            });
        });
        //VX.map.fitBounds(VX.bounds);      //this shows me the ocean east of Africa    
}

So basically my problem is that I can't figure out how to do fitbounds from outside of the .each function, and doing it inside the function calls it for every marker which looks bad.所以基本上我的问题是我无法弄清楚如何从 .each 函数外部进行拟合,而在函数内部执行此操作会为每个看起来不好的标记调用它。

I declare the bounds when I initialize the map... haven't included the entire code because its like 300 lines.我在初始化地图时声明了边界......没有包含整个代码,因为它像 300 行。

Shouldn't I be able to use a value that I passed to a global object?我不应该能够使用我传递给全局对象的值吗?

Edit: ah, I was calling it from outside of the get function!编辑:啊,我是从 get 函数外部调用它的!

The second call doesn't work because it is firing before the ajax get() returns.第二个调用不起作用,因为它在 ajax get()返回之前触发。

Place the fitBounds inside the get() handler, but outside the each() function.fitBounds放在get()处理程序中,但在each()函数之外。 Like so:像这样:

var VX = {
    map:null,
    bounds:null
}
VX.placeMarkers = function(filename) 
{
    $.get
    (
        filename, 
        function(xml) 
        {
            $(xml).find("marker").each
            (
                function() 
                {
                    var lat         = $(this).find('lat').text();
                    var lng         = $(this).find('lng').text();
                    var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));

                    VX.bounds.extend(point);                
                    //VX.map.fitBounds(VX.bounds);    //this works        

                    var marker = new google.maps.Marker
                    ({
                        position: point,
                        map: VX.map,
                        zoom: 10,
                        center: point
                    });
                }
            );
            VX.map.fitBounds(VX.bounds);    //-- This should work.
        }
    );  
    //VX.map.fitBounds(VX.bounds);      //this shows me the ocean east of africa    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM