谷歌地图地理编码器,访问其中的全局变量

时间:2012-07-10 06:17:59

标签: javascript global-variables

我必须从地址中找到latlong,我想在全局数组varibale中加载它们, 在这里我在做什么

  for(i=0; i<locations.length; i++){

    var paddress = locations[i]['address'] +','+locations[i]['postal_code'] + ', ' +locations[i]['district'] + ',' + locations[i]['country'];
    var  pgeocoder = new google.maps.Geocoder();
    pgeocoder.geocode( { 'address': paddress}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            Storelatlong[i] = results[0].geometry.location;

        }
    });
    }


    alert(Storelatlong.length);

但是它总是返回长度0,如果我在地理编码器功能中提醒它可以使latlong很好,有没有其他方法可以将数据从地理编码器内部分配到全局变量,谢谢

1 个答案:

答案 0 :(得分:0)

我认为问题在于,当您从谷歌获得响应时我不再定义,请尝试使用闭包修复它,类似这样的

pgeocoder.geocode( { 'address': paddress}, (function(index) {
      return function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            Storelatlong[index] = results[0].geometry.location;
        };
      }(i))
);