geocoder.getLocations导致var

时间:2012-01-19 12:28:10

标签: javascript jquery

我浪费了很多时间,但我无法做到。

我会感激帮助。

如何在变量中得到逆地理定位的结果?

这是脚本:

geocoder = new GClientGeocoder();
geocoder.getLocations('43.3372814,(-1.79548311)', showAddress);

function showAddress(response) {

    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    return(place.address);
}

我试过:

var address = geocoder.getLocations('43.3372814,(-1.79548311)', showAddress);

但没有结果。

非常感谢你的帮助

1 个答案:

答案 0 :(得分:0)

http://code.google.com/apis/maps/documentation/javascript/v2/services.html所述,第二个参数showAddress是回调函数。所以你必须做类似的事情:

geocoder.getLocations('43.3372814,(-1.79548311)', showAddress);

function showAddress(response) {
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' + 
        '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
  }
}