谷歌地图GetLocation返回字符串

Google Map GetLocation Return String

本文关键字:字符串 返回 GetLocation 谷歌地图      更新时间:2023-09-26

我有一段代码,

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(),但addAddressToMap函数没有更新变量state

感谢

您更新的代码有助于更好地查看图片。

看起来addAddressToMap()正在从函数参数中期待一个响应变量。

geocoder.getLocations(address,addAddressToMap)调用它时,没有传递任何响应。

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

要修复此问题,您需要在调用addAddressToMap()时传递一些内容。它看起来像是脚本中其他地方的XMLHttpRequest(Ajax(对象。