Google Map GetLocation返回字符串

时间:2011-07-01 08:27:54

标签: javascript google-maps

我有这段代码,

geocoder = new GClientGeocoder();
var state;

function addAddressToMap(response) {
  if (!response || response.Status.code != 200) {
    alert("Sorry, we were unable to geocode that address");
  } 
  else {
    place = response.Placemark[0];
    state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
  }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation() {
  var address = "mutiara damansara";
  geocoder.getLocations(address, addAddressToMap);

  return state;
}

好的,更新了代码。我尝试实例化showLocation(),但变量state未由addAddressToMap函数更新。

由于

1 个答案:

答案 0 :(得分:0)

您的更新代码有助于更好地了解图片。

看起来addAddressToMap()期待函数参数的响应变量。

geocoder.getLocations(address,addAddressToMap)调用时,没有传递回复。

因此,第一个if语句!response为真,state仍未设置。

要修复,您需要在致电addAddressToMap()时传递内容。看起来某些东西是脚本中其他地方的XMLHttpRequest(Ajax)对象。