我有一个ajax函数,它返回存储在数据库中的位置的纬度和经度。它们被返回并放在一个数组中。执行计算以基于纬度/经度返回它们与用户当前位置的距离。我想只返回计算距离最短的记录。我的代码如下:
Ajax成功
// 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);
};
}
});
});
}
calculateDistance函数返回用户当前位置与数据库中距离的距离。 varLocationsDistance&lt; = varLocationRadius“If”语句返回特定距离半径(100米)内的记录,在该语句中我想返回最短距离。
我是一名自学成才的业余网站开发人员,因此可能无法提供足够的回复信息,请告知我们。
谢谢,
答案 0 :(得分:1)
修改强>
这甚至可以在一个循环中完成,正如Volune所指出的那样:
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;
}
}
});
更新了demo。
原始回答:
您正在执行许多不必要的操作,尤其是排序,这些操作以后不会在代码中使用。它可以像这样简化:
...
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');
因此,首先我使用$.map()
函数向data
对象添加距离,然后使用$.each()
检查每个位置的距离。这是工作demo。最上面有一个Preparations
部分可以模拟您的数据。