Reputation: 49
I have an ajax function which returns the latitudes and longitudes of locations stored in a database. These are returned and placed in an array. A calculation is performed to return their distance from the users current location based on the latitude/longitude. I would like to return only the record with the shortest calculated distance. My code is as follows:
Ajax Success
// Success
success: function (data) {
// Obtain Log/Lat
navigator.geolocation.getCurrentPosition(function(position) {
// Obtain Current Position Lat/Lon
glbVar.latitude = position.coords.latitude;
glbVar.longitude = position.coords.longitude;
// Console Log
//console.log('Lat: ' + glbVar.latitude + ' Lon: ' + glbVar.longitude);
// Obtain Location Distances
for ( var i = 0; i < data.length; i++ ) {
// Location Instances
var varLocation = data[i];
// Location Distance
varLocation.distance = calculateDistance(glbVar.longitude, glbVar.latitude, varLocation.locationLongitude, varLocation.locationLatitude);
}
// Sort Locations By Distance
var sortedData = data.sort(function(a, b) {
// Return Locations
return a.distance - b.distance;
});
// Output Results
$.map(sortedData, function(item) {
// Obtain Location Distance
varLocationsDistance = calculateDistance(glbVar.longitude, glbVar.latitude, item.locationLongitude, item.locationLatitude);
// Obtain Location Radius Assignment
if (varLocationsDistance <= varLocationRadius) {
// Function Return
functionReturn = $({locationID : item.locationID + ', Distance : ' + varLocationsDistance + ' m'});
// Return
// Function to get the Min value in Array
Array.min = function( sortedData ){
functionReturn = Math.min.apply( Math, sortedData );
//
console.log(functionReturn);
};
}
});
});
}
The calculateDistance function returns the distance from the users current location and those from the database. The varLocationsDistance <= varLocationRadius "If" statement returns records within a certain distance radius (100 meters), within that statement I would like to return the shortest distance.
I am a self taught amateur web developer and as a result may not have provide enough information for an answer, please let me know.
Thanks,
Upvotes: 0
Views: 278
Reputation: 8110
Edit:
This can even be done in a single loop, as Volune pointed out:
var nearestLocation = null;
$.each(data, function(index, location) {
location.distance = calculateDistance(glbVar.longitude, glbVar.latitude, location.locationLongitude, location.locationLatitude);
//two if for readability
if (location.distance <= varLocationRadius) {
if (nearestLocation === null || nearestLocation.distance > location.distance) {
nearestLocation = location;
}
}
});
Updated demo.
Original answer:
You're doing a lot of unnecessary actions, especially sortings, which are not used later in code. It could be simplified like this:
...
glbVar.latitude = position.coords.latitude;
glbVar.longitude = position.coords.longitude;
// Console Log
//console.log('Lat: ' + glbVar.latitude + ' Lon: ' + glbVar.longitude);
var locationsWithDistances = $.map(data, function(location) {
location.distance = calculateDistance(glbVar.longitude, glbVar.latitude, location.locationLongitude, location.locationLatitude);
//console.log(location);
return location;
});
// Obtain Location Distances
var nearestLocation;
$.each(locationsWithDistances, function(index, location) {
if (location.distance <= varLocationRadius && (nearestLocation == undefined || nearestLocation.distance > location.distance)) {
// If current location is nearer, remember it
//console.log(location);
nearestLocation = location;
}
});
if (nearestLocation != undefined)
console.log("LocationID : " + nearestLocation.locationID + ', Distance : ' + nearestLocation.distance + ' m');
So, first I use $.map()
function to add distances to data
object, and then I use $.each()
to check each location's distance. Here is working demo. On top there is a Preparations
section which mocks up your data.
Upvotes: 1