开发者

javascript variable in google maps not available, so confused!

I'm working with google maps v3 and have come across a very perplexing situation using StreetViewService to check for streetview at my marker. Within my 'loadMap' function I'm running this check using:

//more 'loadMap' function code above and below this

var streetViewCheck = new google.maps.StreetViewService();  
streetViewCheck.getPanoramaByLocation(latlng, 50, function(result, status) {
    if (status == google.maps.StreetViewStatus.ZERO_RESULTS) {
        streetViewAvailable = 0;        
    }else{
        streetViewAvailable = 1;
    }
});
alert(streetViewAvailable);

This above alert is throwing this js error message 'streetViewAvailable is not defined' and the variable is not available anywhere in the entire function. However, if I put my alerts here:

var streetViewCheck = new google.maps.StreetViewService();  
streetViewCheck.getPanoramaByLocation(latlng, 50, function(result, status) {
    if (status == google.maps.StreetViewStatus.ZERO_RESULTS) {
        streetViewAvailable = 0;
        alert(streetViewAvailable);     
    }else{
        streetViewAvailable = 1;
        alert(streetViewAvailable);
    }
});

They alert correctly (ie, 0 if streetview not available, 1 if it is), but again the variable is not available anywhere in the function. And now for the truely perplexing part, the variable is available in other functions.

What on earth is going on? Why is my variable not defined within the function that initialises it, but is defined and available in other functions. I'm not the worlds greatest coder (but not the worst eiter!) but this has my completely stumped. I've tried many options, but all with the same result (ie, initialising the variable 'streetViewAvailable' above my new google.maps.StreetViewService(); code, assigning them values of 'yes' and 'no' instead of 1 a开发者_如何学编程nd 0, making them local variables instead of global (var streetViewAvailable...), and all combinations of the just previously mentioned, but all with the same result, not defined in this function.

Please please help me understand this, it also has our other coders stumped, it defies all logic!!! Many many thanks in advance, I hope someone can shead some light on this before my brain implodes!!!


//more 'loadMap' function code above and below this
var streetViewAvailable;

var streetViewCheck = new google.maps.StreetViewService();  
streetViewCheck.getPanoramaByLocation(latlng, 50, function(result, status) {
    if (status == google.maps.StreetViewStatus.ZERO_RESULTS) {
        streetViewAvailable = 0;        
    }else{
        streetViewAvailable = 1;
    }
});
alert(streetViewAvailable);

Try this - you really forgot to declare streetViewAvailable


This is default behaviour for any AJAX call. Your streetViewCheck.getPanoramaByLocation() does an AJAX call to find the data. Because the call is asynchronous, the rest of your code keeps on executing, and the alert is executed before the ajax call is finished. That's why it is empty.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜