从数组中获取最近的纬度和经度

Sim*_*ton 2 javascript arrays loops

我正在尝试编写一个性能导向的数组查找,它将返回相对于用户当前位置最近的经度和纬度.

一个问题是我在我的阵列中有5000个位置,我需要循环所有结果尽可能快地返回最近的结果,我也遇到过滤器的问题,因为它目前正在返回错误的结果(我相信是由于查找只查找最接近当前的负数)

这是我的代码:

    if(has('geolocation')){

            navigator.geolocation.getCurrentPosition((position) => {

                // Example result from getCurrentPosition (this is my exact location)
                //
                // position.coords = {
                //  latitude: 51.4523,
                //  longitude: -0.9724
                // }

                // This is the nearest location
                //
                // location = {
                //  latitude: 51.457495699999996,
                //  longitude: -0.973083
                // }

                // This is the location my filter keeps returning
                //
                // location = {
                //  latitude: "50.6931",
                //  longitude: "-4.0094",
                //  name: "Yes Tor"
                // }

                let closest = this.locations.filter((location) => {
                    console.log(location, position.coords);
                    return location.latitude <= position.coords.latitude && location.longitude <= position.coords.longitude;
                }).pop();


                console.log(closest); //Is incorrect

            });

        }
Run Code Online (Sandbox Code Playgroud)

我已经评论了我目前的结果,我的问题是,是否有更快的方法来做到这一点,如果有一种方法来获得最接近当前的负面和正面.

如果有人需要它,这里是一个数组结构的例子(长度是5968)

            let this.locations = [
                {
                    elevation: "936.0",
                    id: "350001",
                    latitude: "56.8716",
                    longitude: "-4.1969",
                    name: "A' Bhuidheanach Bheag",
                    region: "ta",
                    unitaryAuthArea: "Perth and Kinross"
                },
                {
                    elevation: "999.0",
                    id: "350024",
                    latitude: "57.6926",
                    longitude: "-5.1328",
                    name: "A'Chailleach (Fannaich Region)",
                    region: "he",
                    unitaryAuthArea: "Highland"
                }
            ]
Run Code Online (Sandbox Code Playgroud)

TK *_*ble 5

找到2点之间的距离(Haversine公式):

function distance(position1,position2){
    var lat1=position1.latitude;
    var lat2=position2.latitude;
    var lon1=position1.longitude;
    var lon2=position2.longitude;
    var R = 6371000; // metres
    var ?1 = lat1.toRadians();
    var ?2 = lat2.toRadians();
    var ?? = (lat2-lat1).toRadians();
    var ?? = (lon2-lon1).toRadians();

    var a = Math.sin(??/2) * Math.sin(??/2) +
        Math.cos(?1) * Math.cos(?2) *
        Math.sin(??/2) * Math.sin(??/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

    var d = R * c;
    return d;
}
Run Code Online (Sandbox Code Playgroud)

现在使用:

var closest=locations[0];
var closest_distance=distance(closest,position.coords);
for(var i=1;i<locations.length;i++){
    if(distance(locations[i],position.coords)<closest_distance){
         closest_distance=distance(locations[i],position.coords);
         closest=locations[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

这具有O(n)的复杂性.5000个位置,大部分时间都足够好.