4
I have a function in ajax that returns all latitudes and longitudes of users registered in the database and gives me the shortest distance between the coordinates returned and a fixed coordinate in the code , works very well it gives me the lowest value, the problem is that I need to know which is the ID of this user that has the least distance coordinate, I have no idea how to do it , I tried to give a Return in the ID but it returns the smallest ID and not the ID of the smallest coordinate.
Here is my ajax code
$.ajax({
type : 'POST',
data : formula,
url : 'http://10.0.0.119/melleve/statusorig2.php',
success : function(data){
var retorno = JSON.parse(data);
var testandoisso = $.map(retorno, function (value) {
var latitude = value.latitude;
var longitude = value.longitude;
var id = value.idmoto;
var lat1 = -22.8650697;
var lat2 = latitude;
var long1 = -43.287510499999996;
var long2 = longitude;
var radlat1 = Math.PI * lat1/180;
var radlat2 = Math.PI * lat2/180;
var theta = long1 - long2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1)*Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
var finaldist = dist * 1.609344;
return finaldist;
});
var ok = Math.min.apply(Math, testandoisso);
console.log(ok);
},
error: function(jqXHR, textStatus, errorThrown){
//debugger;
}
//,complete: function(jqXHR, textStatus){
// debugger;
//}
})
from now , many thanks to all.
console.log(ok) writes to my browser console the value of the shortest distance, I need it to return me the ID of who has the shortest distance.
the our. nice.
– Lollipop
very good works properly, thanks friend.
– Weverton Souza