开发者

How to make multiple calls from a feed using getJSON

开发者 https://www.devze.com 2023-03-29 07:42 出处:网络
I\'m trying to make a loop to get wheather conditions from diferent cities and show them in a googlemap as markers.

I'm trying to make a loop to get wheather conditions from diferent cities and show them in a googlemap as markers. If I do this I can get one at a time:

$.getJSON("http://free.worldweatheronline.com/feed/weather.ashx?q="+markers[0].lat+","+markers[0].lng+"&format=json" +"&num_of_days=1" + "&key=e678d973c7184412112807"+"&callback=?",function(jsonp){
        $.each(jsonp.data.current_condition, function(i){
            var time = this;
            markerWeather[0] = new google.maps.Marker({
                position: new google.maps.LatLng(markers[0].lat,markers[0].lng),
                title: "Temp. " + markers[0].title + ": "+ time.temp_C + " grados",
                map: mapa
            });      
        });
    });

And if I copy all that and change the number it works with all my markers... the problem is that if I exchange the number and put a variable instead with a loop, it doesn't work well.. example:

for(z=0; z<=5; z++){  
$.getJSON("http://free.worldweatheronline.com/feed/weather.ashx?q="+markers[z].lat+","+markers[z].lng+"&format=json" +"&num_of_days=1" + "&key=e678d973c7184412112807"+"&callback=?",function(jsonp){
                $.each(jsonp.data.current_condition, function(i){
                    var time = this;
                    markerWeather[0] = new google.maps.Marker({
                        position: new google.maps.LatLng(markers[z].lat,markers[z].lng),
                        title: "Temp. " + markers[z].title + ": "+ time.temp_C + " grados",
                        map: mapa
                    });      
                });
            });
}

Help please! And sorry for the engrish.

PS: this is my page: www.kanawogirusa.开发者_运维技巧com.ar PS2: and here is the code map.js


Your problem is that you are assigning your markers[z] to markerWeather[0] .. so everything is assigned to [0]

At least that's the most obvious thing I can spot right now..

UPDATE

OK I guess I found the problem. Since .getJson is a async call the method has already returned and your for() loop has already modified the variable 6 times when the first async call comes back to execute the callback.

what you need to do is this:

var f = function(z) {
        $.getJSON("http://free.worldweatheronline.com/feed/weather.ashx?q="+markers[z].lat+","+markers[z].lng+"&format=json" +"&num_of_days=1" + "&key=e678d973c7184412112807"+"&callback=?",function(jsonp){
                        $.each(jsonp.data.current_condition, function(i){
                            var time = this;
                            markerWeather[z] = new google.maps.Marker({
                                position: new google.maps.LatLng(markers[z].lat,markers[z].lng),
                                title: "Temp. " + markers[z].title + ": "+ time.temp_C + " grados",
                                map: mapa
                            });      
                        });
                    });
    }

for(z=0; z<=5; z++){  
    f(z);
}

By calling a function on each loop iteration you retain the z variable through the closure..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号