fxnGetNearestDealer: function () {
//Gets LatLng for given Zipecode and displays the dealer.
fxnGetLatLngforZip(function () {
//Gets The Nearest Dealers Index out of dealers in the site.
fxnGetNearDealerIndex(function (distance) {
alert(distance);
//Gets Html To Display NearestDealer Details.
sHtml = fxnGetHtmlforNearestDealer();
//Displays Nearest Dealer Details in the span with Id "spanDNAddr".
document.getElementById('spanDNAddr').innerHTML = sHtml;
});
});
};
fxnGetLatLngforZip = function (callback) {
var oGeocoder = new google.maps.Geocoder(),
iZipcode = document.getElementById('txtZipCode').value;
//Boundary checks
if (!iZipcode) { return; }
oGeocoder.geocode({ 'address': iZipcode }, function (result, status) {
if (status == google.maps.GeocoderStatus.OK) {
g_oLatLng = result[0].geometry.location;
callback();
}
else {
//Can use status its self to display the error message.
document.getElementById('spanDNAddr').innerHTML = "No Results Found Enter Valid ZipCode";
}
});
};
fxnGetNearDealerIndex = function (callback) {
//Boundary Checks
if (!g_oLatLng) { return; }
var oDealerLatlng = null,
dDistance = null,
tempindex = null,
dTemp = null;
for (var iAddrIdx = 0; iAddrIdx < g_oAddrs.length; iAddrIdx++) {
oRequest = {
origin: g_oLatLng,
destination: new google.maps.LatLng(g_oAddrs[iAddrIdx].latitude, g_oAddrs[iAddrIdx].longitude),
travelMode: google.maps.TravelMode.DRIVING
};
g_oDirections.route(oRequest, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
dDistance = response.routes[0].legs[0].distance.value;
if (!dTemp) {
dTemp = dDistance;
}
else if (dDistance < dTemp) {
dTemp = dDistance;
g_iNearIndex = iAddrIdx;
}
}
});
}
callback(dTemp);
};
在上面的函数中“fxnGetNearestDealer”是从那里的起点,我试图为给定的邮政编码获取 latlng,因为这将是一个异步调用,我使用回调它工作正常..之后我必须进行另一个异步调用来计算之间的行驶距离给定邮政编码 latlng 并且每个 latlng 在 forloop 中迭代..并获得最小值。我最终像上面那样写了....问题它永远不会返回任何值它在警报中给出 null。如果我在萤火虫中看到它会迭代所有创建好的请求,但它永远不会进入“g_oDirections.route”函数,因为它是异步的,我使用了回调但它没有工作....任何解决方法请... ...