通过当前位置节点js获取圈子中最近的用户 [英] Get nearest users within a circle by current location node js

查看:53
本文介绍了通过当前位置节点js获取圈子中最近的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在举例说明以达到预期效果.

I'm explaining with an example to achieve my result.

我有一个用户表.每个用户在数据库中都有其经度和纬度的位置.我正在使用Firebase数据库.因此数据库看起来像:

I have a users table. Each user has their location with latitude and longitude on the database. I'm using firebase database. So the DB looks like:

users {
  user-id{
     location{
        latitude
        longitude
     }
  }
}

我需要使用当前位置在一个圈子内找到用户.

I need to find users within a circle using my current location.

我研究了 geolib ,并找到了一种方法

I've looked into geolib and found a method isPointInCircle . But using this, we need to iterate users data and needs to call this method on each loop. I need to avoid this multiple calls and achieve with only one method call like:

findNearestUsers(currentLocation,userLocations);

结果应为具有最近位置的数组.我怎样才能做到这一点?在此先感谢:)

The result should be an array having nearest locations. How can I do this? Thanks in advance :)

推荐答案

GeoFire是基于与特定点的接近度查询Firebase实时数据库的唯一方法,而无需下载所有数据并过滤客户端.

GeoFire is the only way to query the Firebase Realtime Database based on proximity to a specific point, without having to download all data and filtering client side.

使用GeoFire,您创建 GeoQuery获取位置和最大距离:

With GeoFire you create a GeoQuery for a location and a maximum distance:

var geoQuery = geoFire.query({
  center: [10.38, 2.41],
  radius: 10.5
});

然后,您为要处理以下内容的项目附加处理程序处于此范围内:

var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
  console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});

没有内置功能可查找单个最接近的物品.您可以根据回调中的 distance 参数在客户端进行操作.您仍然会检索比所需更多的数据,但希望它会少于整个数据库.

There is no built-in functionality for finding a single, closest item. You could do that client-side based on the distance parameter in the callback. You'd still be retrieving more data than needed, but hopefully it'd be less than the entire database.

这篇关于通过当前位置节点js获取圈子中最近的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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