通过过滤按最接近的地理位置对数据进行排序 [英] Sort data by closest geolocation with filtering

查看:88
本文介绍了通过过滤按最接近的地理位置对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到某人正在使用我的应用程序的正确建筑物.现在,我已经进行了非常糟糕的过滤,我需要对其进行改进.

I am trying to locate correct building where someone is using my application in. Right now i have managed to do a pretty bad filtering and i need to improve it.

我的目标是通过过滤将最接近的建筑物归还给使用该应用程序的人,因此,在同一建筑物中四处走动时,尽可能对其进行尽可能好的过滤而不会造成错误.

What my goal is with filtering to return the closest building to the person using the application, so filter it as good as possible without creating errors when moving around in the same building.

我的提取返回一个如下所示的api JSON数组:

My fetch returns an api JSON array that looks like this:

{
"data": [
{
  "id": 1,
  "city": "CITY",
  "building_name": "Building 1",
  "building_address": "Address 123",
  "latitude":  "57.7052809",
  "longitude": "16.9367817"
},
{
  "id": 2,
  "city": "CITY",
  "building_name": "Building 2",
  "building_address": "Address 456",
  "latitude":  "35.7054509",
  "longitude": "16.9366141"
}
],
}

这是我的代码

fetch('http://localhost:8888/api/buildings')
.then(response => response.json())
.then(data => {

  userCoordinates = {
    latitude:  35.7053509,
    longitude: 16.9362301
  }


  const returnedBuilding = Object.entries(data.data).map(([inst, key]) => key)
  .filter(thing => (thing.latitude > userCoordinates.latitude - .5 &&
     thing.latitude < userCoordinates.latitude + .5) &&
      (thing.longitude > userCoordinates.longitude -.5 &&
       thing.longitude < userCoordinates.longitude + .5));

       console.log(returnedBuilding);

})

推荐答案

在两点之间使用距离"来找到最接近的点.您不能保证用户的建筑物,但是可以保证相对于用户坐标最接近的建筑物.

Use 'distance' between two points to find the closest. You can't guarantee the user's building but you can guarantee which one is closest with respect to user's co-ordinate.

var keys = Object.entries(data.data).map(([inst, key]) => key);
var returnedBuilding = 
  keys.reduce((prevCord, thing) => {
    var dist = getDistance(userCoordinates.latitude, userCoordinates.longitude, thing.latitude, thing.longitude);
    var prevDist = getDistance(userCoordinates.latitude, userCoordinates.longitude, prevCord.latitude, prevCord.longitude);
    return dist < prevDist? thing : prevCord;
}, keys[0]);

下面是getDistance函数,我从这篇文章引用.但是,您可以编写自己的,更简单的代码,因为您只处理小距离(我想).

Below is the getDistance function, I referred from this post. However you can write your own, a simpler one as you are dealing with small distances only (I assume).

function getDistance(lat1, lon1, lat2, lon2) 
{
  var R = 6371; // km
  var dLat = toRad(lat2-lat1);
  var dLon = toRad(lon2-lon1);
  var lat1 = toRad(lat1);
  var lat2 = toRad(lat2);

  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c;
  return d;
}

// Converts numeric degrees to radians
function toRad(Value) 
{
    return Value * Math.PI / 180;
}

这篇关于通过过滤按最接近的地理位置对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆