Return ID of who has the shortest distance between coordinates

Asked

Viewed 98 times

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.

2 answers

6


The min.apply will not help you in this case because it returns only one value. You would have to create a structure with distância, id, and return both.

See a functional example:
(I wiped the code so I could show it in Sopt himself)

var retorno = [
    { "latitude":-23, "longitude":-49, "idmoto":1 },
    { "latitude":-22, "longitude":-47, "idmoto":2 },
    { "latitude":-25, "longitude":-49, "idmoto":3 },
    { "latitude":-22, "longitude":-43, "idmoto":4 }, // O mais próximo do Botafogo - N.I.
    { "latitude":-24, "longitude":-48, "idmoto":5 }, 
    { "latitude":-21, "longitude":-40, "idmoto":6 },
    { "latitude":-22, "longitude":-48, "idmoto":7 },
    { "latitude":-20, "longitude":-38, "idmoto":8 }
  ];
  
var testandoisso = retorno.map( function (value) {
  var lat1  = -22.8650697;
  var long1 = -43.287510499999996;

  var lat2  = value.latitude;
  var long2 = value.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.acos( Math.sin(radlat1) * Math.sin(radlat2)
    + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta) )
    * (180/Math.PI)  * 60 * 1.1515 * 1.609344;

  return { 'distancia':dist, 'id':value.idmoto };
} );

var escolhido = { 'distancia':Infinity, 'id':0 };
for(var i = 0; i < testandoisso.length; i++ ) {
  if ( testandoisso[i]['distancia'] < escolhido['distancia'] ) escolhido = testandoisso[i];
}

// Mostrando o resultado:
document.body.innerHTML += 'Id:'   + escolhido['id']        + '<br>';
document.body.innerHTML += 'Dist:' + escolhido['distancia'] + '<br>';

  • the our. nice.

  • 1

    very good works properly, thanks friend.

0

You can save the value of the user ID to an external variable, which will be updated every time your function finds the shortest distance, so when you call the function and it returns the value, you can access the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.