【发布时间】:2012-05-10 16:30:58
【问题描述】:
所以我最近遇到了一个问题,也许你们可以帮忙。
首先,我创建了网站和标记,并尝试检索中心点以对地址进行反向地理编码。
我的代码如下:
function ReverseGeocode(lat, lng)
{
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"latLng": latlng}, function(results, status)
{
if (status != google.maps.GeocoderStatus.OK)
{
alert("Geocoding has failed due to "+ status);
}
address = results[0].formatted_address;
});
}
我在这里遇到的问题是,当我尝试将“地址”传回时(地址现在是一个全局变量),我得到的只是“未定义”。
这是我试图将其传回的代码: sendString += '&lat=' + lat; sendString += '&lng=' + lon; ReverseGeocode(center.lat(), center.lng()); alert(""+地址);
sendString += '&address=' + address
var currentLang = "en"
sendString += '&phone=' + document.getElementById("number").value;
sendString += '&email=' + document.getElementById("email").value;
sendString += ($("prefsms").checked)?'&contactMethod=sms':'&contactMethod=email';
sendString += '&serviceType=' + document.getElementById("serviceType").value;
sendString += '&language=' + currentLang;
alert(""+sendString);
在我的警告框中,我得到的只是“未定义”。然而,如果我在 ReverseGeocode 函数中添加另一个警报框,我将在警报框中获取地址,但这会发生在外部函数中的警报框之后。
关于发生了什么的任何想法?我原以为 ReverseGeocode 函数中的警报框会先出现,而不是相反。
谢谢!
【问题讨论】:
标签: javascript google-maps geocoding