我在json中有一个地址列表,我想迭代并查找单个lon / lat(html5地理定位)的距离。我希望找到距离最短的地址(json项目)并稍后再做一些事情。
我使用jQuery.cookie库和jquery 1.10
我的设置问题是,DistanceMatrixService回调是异步的,并在负责向用户显示最近地址的函数之后返回。
代码如下所示:
HTML5地理位置代码:
function get_user_location() {
window.onload = function () {
var html5Options = { enableHighAccuracy: true, timeout: 6000, maximumAge: 0 };
geolocator.locate(onGeoSuccess, onGeoError, true, html5Options);
};
function onGeoSuccess(location) {
$.cookie('user_postal_code', location.address.postalCode);
$.cookie('user_latitude', location.coords.latitude);
$.cookie('user_longitude', location.coords.longitude);
get_hca_rss();
}
function onGeoError(error) {
console.log(error);
}
}
get_user_location();
jSON pull:
function get_hca_rss() {
var hcafeed = 'http://hcafeeds.medcity.net/rss/er/rss_feed.xml';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from rss where url="' + hcafeed + '"') + '&format=json&diagnostics=true&callback=?';
$.getJSON(yql,function(data){
if(data) {
get_closest_er(data);
}
});
}
获取最近的地址:
function get_closest_er(data, userLocation) {
var service = new google.maps.DistanceMatrixService();
var rows = data.query.results.item;
var origin = $.cookie('user_latitude') + "," + $.cookie('user_longitude');
$.each(rows,function(e) {
if(rows[e].address != undefined) {
var destination = rows[e].address;
tmp = Number.POSITIVE_INFINITY;
closestIndex = null;
// Google Distance Matrix API
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix( {
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
durationInTraffic: false,
avoidHighways: false,
avoidTolls: false,
}, callback);
// Callback for distance api
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var distance = response.rows[0].elements[0].distance.value;
if (distance < tmp) {
tmp = distance;
$.cookie('closestIndex',e);
$.cookie('closestDistance',response.rows[0].elements[0].distance.text);
}
}
}
}
});
set_closest_er_html(data);
};
设置HTML:
function set_closest_er_html(data) {
var rows = data.query.results.item;
var hElem = $('#closestER');
var uZip = $.cookie('user_postal_code');
var cIdx = $.cookie('closestIndex');
var cHospital = rows[cIdx];
var cName = cHospital.comments.replace(" ER Wait Time", "");
var cDistance = $.cookie('closestDistance').replace(" mi", "");
var cWaitTime = cHospital.description.replace(" Mins", "");
hElem.html('<h2>Hospital Info:</h2>');
hElem.append('<div><span>Your Closest ER </span><span>' + uZip + ' Zip Code</span></div>');
hElem.append('<div><strong>Hospital </strong><span>' + cName + '</span></div>');
hElem.append('<div><label>Distance: </label><div>' + cDistance + '</div> Miles</div>');
hElem.append('<div><label>Wait: </label><div>' + cWaitTime + '</div> Minutes</div>');
}
我想问题是......如何等到循环中的最后一个callback()函数完成并且在进入set_closest_er_html函数之前设置最近的地址cookie?
提前致谢。