Javascript 通过过滤按最近的地理位置对数据进行排序

Javascript 通过过滤按最近的地理位置对数据进行排序,javascript,jquery,Javascript,Jquery,我正试图找到有人在其中使用我的应用程序的正确建筑物。现在我已经设法做了一个非常糟糕的过滤,我需要改进它 我的目标是通过过滤将最近的建筑物返回给使用该应用程序的人,以便在同一建筑物中移动时尽可能地进行过滤而不产生错误 My fetch返回一个api JSON数组,如下所示: { "data": [ { "id": 1, "city": "CITY", "building_name": "Building 1", "building_address": "Address 123",

我正试图找到有人在其中使用我的应用程序的正确建筑物。现在我已经设法做了一个非常糟糕的过滤,我需要改进它

我的目标是通过过滤将最近的建筑物返回给使用该应用程序的人,以便在同一建筑物中移动时尽可能地进行过滤而不产生错误

My fetch返回一个api JSON数组,如下所示:

{
"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"
}
],
}
{
  "id": 1,
  "city": "CITY",
  "building_name": "Building 1",
  "building_address": "Address 123",
  "location": {
    "type": "Point",
    "coordinates": [ // IMPORTANT: note the order is longitude, latitude
      16.9367817, // longitude
      57.7052809, // latitude
    ]
  }
}
这是我的密码

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);

})
fetch('http://localhost:8888/api/buildings')
.then(response=>response.json())
。然后(数据=>{
用户坐标={
纬度:35.7053509,
经度:16.9362301
}
const returnedBuilding=Object.entries(data.data).map([inst,key])=>key)
.filter(thing=>(thing.latitude>userCoordinates.latitude-.5&&
thing.latitudeuserCoordinates.longitude-.5&&
thing.longitude
您可以使用公式确定最近的建筑

哈弗森公式确定了给定经度和纬度的球体上两点之间的大圆距离。tl;博士,你可以这样做,找到你自己和建筑物之间的距离,然后准确地确定哪一个是最近的

下面是SO的另一篇关于在javascript中使用它的文章:

您可以使用公式确定最近的建筑

哈弗森公式确定了给定经度和纬度的球体上两点之间的大圆距离。tl;博士,你可以这样做,找到你自己和建筑物之间的距离,然后准确地确定哪一个是最近的

下面是SO的另一篇关于在javascript中使用它的文章:

你可以在每栋建筑中循环,根据距离找到最近的一栋

var closestBuilding = building[0];
var closestDistance = distanceFrom(user, building);
building.forEach(function(building) {
  var distanceFrom = distanceFrom(user, building);
  if(closestDistance > distanceFrom){
    closestBuilding = building;
    closestDistance = distanceFrom;
  } 
})
console.log(closestBuilding);

function distanceFrom(user, building) { /* your distance code */ }

你可以在每个建筑中循环,根据距离找到最近的一个

var closestBuilding = building[0];
var closestDistance = distanceFrom(user, building);
building.forEach(function(building) {
  var distanceFrom = distanceFrom(user, building);
  if(closestDistance > distanceFrom){
    closestBuilding = building;
    closestDistance = distanceFrom;
  } 
})
console.log(closestBuilding);

function distanceFrom(user, building) { /* your distance code */ }

我强烈建议您考虑更改数据模型,以利用mongoDB数据库中存储的点。这将允许您利用mongoDB强大的内置功能。使用上述现有数据模型作为起点,您的新数据模型可以如下所示:

{
"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"
}
],
}
{
  "id": 1,
  "city": "CITY",
  "building_name": "Building 1",
  "building_address": "Address 123",
  "location": {
    "type": "Point",
    "coordinates": [ // IMPORTANT: note the order is longitude, latitude
      16.9367817, // longitude
      57.7052809, // latitude
    ]
  }
}
上面对象中的location属性是GeoJSON点。现在,您不必从数据库中获取每个位置并自己在客户机中进行计算,而应该查询您的建筑物数据库,查找距离用户位置最近的建筑物。假设用户坐标为
纬度:35.7053509
经度:16.9362301
,则您的查询可能如下所示(通过GET请求):

http://localhost:8888/api/buildings?lat=35.7053509&lng=16.9362301&maxDistance=2000

mongoDB文档提供了如何处理地理空间查询的示例。此示例取自文档,是api处理请求的方式:

// If you're using express.js, pull the params off the query object
const { lat, lng, maxDistance } = req.query;
const buildings = db.buildings.find(
  {
    location:
      {
        $near:
          {
            $geometry: { type: "Point", coordinates: [lng, lat] },
            $maxDistance: maxDistance
          }
      }
  }
)
  .then(buildings => res.json({ buildings })) // respond to the client
  .catch(err => console.log(err)) // do something with the error
来自服务器的响应将是指定
maxDistance
范围内所有建筑物的列表,按与用户位置的距离排序(最近到最远)。MongoDB地理空间查询的速度和性能令人难以置信。如果客户端只需要一个结果,您甚至可以从
db.find
操作中分割第一个结果,并从api返回一个建筑

希望这是有意义和帮助的!如果您没有使用mongoDB和/或geoJSON对象,一开始可能会觉得有点吓人,但是相信我,这会让您的生活变得更加轻松。在设置数据库和集合时,可能会出现另一个小问题。您需要确保在“建筑物”集合中添加索引以支持地理空间查询。从文件中:

db.buildings.createIndex({location:“2dsphere”})

然后创建集合并向其中添加建筑文档


如果您需要任何澄清,请随时跟进。我建议您阅读mongoDB的文档并在线搜索更多示例。

我强烈建议您考虑更改数据模型,以利用mongoDB数据库中存储的点。这将允许您利用mongoDB强大的内置功能。使用上述现有数据模型作为起点,您的新数据模型可以如下所示:

{
"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"
}
],
}
{
  "id": 1,
  "city": "CITY",
  "building_name": "Building 1",
  "building_address": "Address 123",
  "location": {
    "type": "Point",
    "coordinates": [ // IMPORTANT: note the order is longitude, latitude
      16.9367817, // longitude
      57.7052809, // latitude
    ]
  }
}
上面对象中的location属性是GeoJSON点。现在,您不必从数据库中获取每个位置并自己在客户机中进行计算,而应该查询您的建筑物数据库,查找距离用户位置最近的建筑物。假设用户坐标为
纬度:35.7053509
经度:16.9362301
,则您的查询可能如下所示(通过GET请求):

http://localhost:8888/api/buildings?lat=35.7053509&lng=16.9362301&maxDistance=2000

mongoDB文档提供了如何处理地理空间查询的示例。此示例取自文档,是api处理请求的方式:

// If you're using express.js, pull the params off the query object
const { lat, lng, maxDistance } = req.query;
const buildings = db.buildings.find(
  {
    location:
      {
        $near:
          {
            $geometry: { type: "Point", coordinates: [lng, lat] },
            $maxDistance: maxDistance
          }
      }
  }
)
  .then(buildings => res.json({ buildings })) // respond to the client
  .catch(err => console.log(err)) // do something with the error
来自服务器的响应将是指定
maxDistance
范围内所有建筑物的列表,按与用户位置的距离排序(最近到最远)。MongoDB地理空间查询的速度和性能令人难以置信。如果客户端只需要一个结果,您甚至可以从
db.find
操作中分割第一个结果,并从api返回一个建筑

希望这是有意义和帮助的!如果您没有使用mongoDB和/或geoJSON对象,一开始可能会觉得有点吓人,但是相信我,这会让您的生活变得更加轻松。在设置数据库和集合时,可能会出现另一个小问题。您需要确保在“建筑物”集合中添加索引以支持地理空间查询。从文件中:

db.b