I use this code to calculate to distance between two gps positions. Problem is when i return calculated value it returns undefined value. Please help me
function calcDistane(offerID,userLocation){
var dist;
var adapter = new LocationAdapter();
adapter.selectByOfferId(offerID,function(res){
navigator.geolocation.getCurrentPosition(function(position){
var R = 6371;
var userLocation= position.coords;
dist= Math.acos(Math.sin(userLocation.latitude)*Math.sin(res.item(0).lt) +
Math.cos(userLocation.latitude)*Math.cos(res.item(0).lt) *
Math.cos(userLocation.longitude-res.item(0).lg)) * R;
});
});
return dist;
};
distisn’t set yet when you return. The function that is setting dist is a callback. That is very likely to be called after you return from the outer (callback) function.The likely order of execution is
You will need to pass a continuation rather than returning