在Google Map Api v3反向地理编码上未定义

时间:2012-08-13 04:00:37

标签: javascript google-maps-api-3

  

可能重复:
  Undefined is not a function, Google Geolocation

我正在尝试谷歌反向地理编码的位置,它返回我未定义但在console.log上显示地址

这是获取值的函数

function getLatLng(latlng, callback) {
    var codedAddress;

    geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            codedAddress = results[1].formatted_address;
            console.log("codedAddress 1 = "+codedAddress);
        } else {
            alert("There was a problem with the map");
        }
        console.log("codedAddress 2 = "+codedAddress);
    });
    callback(codedAddress);
}

这是我的初始代码

function initialize() {
    var mapOptions = {
        zoom: 11,
        center: new google.maps.LatLng(5.386, 100.245),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    var location_coords = [
        <?php echo $output; ?>
    ];

    var location_status = [
        <?php echo $status; ?>
    ];

    var location_users = [
        <?php echo $users; ?>
    ];

    var location_date = [
        <?php echo $date; ?>
    ];

    for(i=0;i<location_coords.length;i++) {
      var traffic_options = {
        strokeColor: '#A52A2A',
        strokeOpacity: 0.8,
        strokeWeight: 1,
        fillColor: getColor(location_status[i]),
        fillOpacity: 0.5,
        map: map,
        center: location_coords[i],
        radius: 300
      };
      cityCircle = new google.maps.Circle(traffic_options);

    barr[i] = new Object;

    var marker = new google.maps.Marker({
        position: location_coords[i],
        map: map,
    });

    barr[i].marker = marker;
    barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" +
        "Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; }) + 
        "<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>";

    barr[i].infoWindow = new google.maps.InfoWindow({
        content: barr[i].html
    });

    barr[i].listener = makeClosure(i, barr[i].marker);
    }
}

问题出在本声明中

"Traffic Location: " + getLatLng(location_coords[i], function(codedAddress) { return codedAddress; })

我如何获得此处的值而不是“未定义”

谢谢

2 个答案:

答案 0 :(得分:0)

您需要将回调移动到google地图回调函数中:

function getLatLng(latlng, callback) {
  var codedAddress;

  geocoder.geocode({'latLng': new google.maps.LatLng(40.730885,-73.997383)}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        codedAddress = results[1].formatted_address;
        console.log("codedAddress 1 = "+codedAddress);
      } else {
        alert("There was a problem with the map");
      }
      console.log("codedAddress 2 = "+codedAddress);
      callback(codedAddress); //moved here
  });
  //it was here
}

因为您在Google API填充变量之前调用了回调。

编辑: 您还需要修改写出位置的代码,因为假定函数调用是同步的。

e.g。类似的东西:

getLatLng(location_coords[i], function(codedAddress) {
    barr[i].html = "<div><span style='font-size:9px'>Reported by " + location_users[i] + " on " + location_date[i] + "</span>" +
      "Traffic Location: " + codedAddress;  + 
      "<p>Traffic Condition: " + getCondition(location_status[i]) + "</p></div>";
});

答案 1 :(得分:0)

在getLatLng()(doh,它是......)中,您需要将callback(codedAddress);移动到地理编码调用中(并使用latlng参数):

function getLatLng(latlng, callback) {
  var codedAddress;

  geocoder.geocode({'latLng': latlng}, 
    function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        codedAddress = results[1].formatted_address;
        console.log("codedAddress 1 = "+codedAddress);
      } else {
        alert("There was a problem with the map");
      }
      console.log("codedAddress 2 = "+codedAddress);
    }

    callback(codedAddress);
  );      
}

此函数将返回void,因此您的回调应初始化barr对象。