我有一个在ajax请求的成功事件中生成的数组。从ajax请求获得的数组如下所示:
阵列:
[{
"locationID": "9",
"locationName": "Employee Residence",
"locationLatitude": "34.47189",
"locationLongitude": "-111.9896046999",
}, {
"locationID": "40",
"locationName": "Tron Utah",
"locationLatitude": "33.964212",
"locationLongitude": "-118.3783589999",
}, {
"locationID": "39",
"locationName": "Tron Enterprises",
"locationLatitude": "33.735187",
"locationLongitude": "-111.9579499999",
}]
在此阵列上执行计算,该阵列提供位置列表及其与用户当前位置的距离。我想按距离对结果进行排序;我的代码如下:
Jquery的:
// 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);
// Index Location Return Data
for (var index = 0; index < data.length; index++) {
// Items
var item = data[index];
// Obtain Location Details
varLocationDetails[index] = {"Location Position": {"Longitude": item.locationLongitude, "Latitude": item.locationLatitude, "Location": item.locationName}};
// Each Location Detail
$.each(varLocationDetails[index], function (a,b) {
// Each Locations Distance (From Users Current Location)
varLocationsDistance = calculateDistance(glbVar.longitude, glbVar.latitude, b.Longitude, b.Latitude);
// Determine Locations Radius
if (varLocationsDistance <= varLocationRadius) {
// Determine Distance For Each Location
varDistanceObject[varInt] = {distance: varLocationsDistance, location: b.Location};
// Sort by Distance
//varDistanceObject[varInt].sort(function(a,b) {
// return parseInt(a.distance) - parseInt(b.distance)
//});
// Console Log
//console.log(varDistanceObject[varInt]);
// Obtain Location Name/Distance
varLocation = b.Location;
varDistance = calculateDistance(glbVar.longitude, glbVar.latitude, b.Longitude, b.Latitude);
// Obtain Function Return
functionReturn = '<li>' + varLocation + ': ' + varDistance + 'm</li>';
// Console Log
//console.log(functionReturn);
// Function Return
$('#groups').append(functionReturn);// Append to HTML
};
// Increment
++varInt;
});
}
});
}
距离计算
// Calculate Distance
function calculateDistance(varLongitude, varLatitude, varLon, varLat) {
// Assign Radius
varRadius = 6371;
// Obtain Lon/Lat Values
varLongitude = varLongitude * (Math.PI / 180);
varLatitude = varLatitude * (Math.PI / 180);
varLon = varLon * (Math.PI / 180);
varLat = varLat * (Math.PI / 180);
// 1st X/Y Axis
x0 = varLongitude * varRadius * Math.cos(varLatitude);
y0 = varLatitude * varRadius;
// 2nd X/Y Axis
x1 = varLon * varRadius * Math.cos(varLat);
y1 = varLat * varRadius;
// Calculate Values
dx = x0 - x1;
dy = y0 - y1;
// Determine Distance
d = Math.sqrt((dx * dx) + (dy * dy));
// Function Return
return Math.round(d * 1000);
};
当前结果:
Residence: 0m
Tron Utah: 532559m
Tron Enterprises: 45358m
期望的结果:
Residence: 0m
Tron Enterprises: 45358m
Tron Utah: 532559m
&#34;按距离排序&#34;部分代码被注释掉,因为它不起作用。任何帮助纠正此代码或提供替代方法的人都表示赞赏。我是一名自学成才的业余网站开发人员。
谢谢,
答案 0 :(得分:3)
首先将所有距离值插入数据数组中。所以你有这样的数据:
{
"locationID": "9",
"locationName": "Employee Residence",
"locationLatitude": "34.47189",
"locationLongitude": "-111.9896046999",
"distance": 45456
}
这是通过迭代这样的给定数据来完成的:
for ( var i = 0; i < data.length; i++ ) {
var entry = data[i];
entry.distance = calculateDistance(entry.locationLongitude, entry.locationLatitude, LOCAL_LON, LOCAL_LAT);
}
按距离属性对数据进行排序。
var sortedData = data.sort(function(a, b) {
return a.distance - b.distance;
});
现在你将有一个按距离排序的数组
遍历已排序的数组并创建您的html表示