我不知道有任何方法可以自动执行此操作(以类似 SQL 的方式),但您可以尝试以下操作(请参阅代码中的注释):
现场示例在这里。
/* Declare utility function to convert degrees to radians */
function radians(deg) {
return deg * Math.PI / 180;
}
/* Declare utility function to compute distance from (37, -122) */
/* (If these values change dynamically they should */
/* be given as parameters to the function.) */
var cos37 = Math.cos(radians(37));
var sin37 = Math.sin(radians(37));
function distance(lat, long) {
var dist = 3959
* Math.acos(cos37
* Math.cos(radians(lat))
* Math.cos(radians(long)
- radians(-122))
+ sin37
* Math.sin(radians(lat)));
return dist;
}
/* Somehow populate a 2-dimensional array containing all entries. */
/* Each row should look like this: [id, latitude, longtitude] */
var allArr = ...; // [[id1, lattitude1, longtitude1],[id2, lattitude2, longtitude2],...]
/* Populate a new array containing [id, distance] of those entries */
/* whose distance from (37, -122) is less than 12925. */
var newArr = [];
for (var i = 0; i < allArr.length; i++) {
var dist = distance(allArr[i][1]/*latitude*/,
allArr[i][2]/*longtitude*/);
if (dist < 12925) {
newArr.push([allArr[i][0]/*id*/,
dist]);
}
}
我还注意到,在地球上不可能找到任何两个距离超过 12925 英里的地方,所以除非你正在设计一些外星网络应用程序,否则你可能需要修改你的规格:)