通过当前位置节点js获取圈内最近的用户
Get nearest users within a circle by current location node js
我正在用一个例子来解释以实现我的结果。
我有一个用户table。每个用户在数据库中都有他们的位置和经纬度。我正在使用 Firebase 数据库。所以数据库看起来像:
users {
user-id{
location{
latitude
longitude
}
}
}
我需要使用我当前的位置在一个圈子内找到用户。
我调查了 geolib and found a method isPointInCircle 。但是使用这个,我们需要迭代用户数据并且需要在每个循环中调用这个方法。我需要避免这种多次调用并仅通过一个方法调用来实现,例如:
findNearestUsers(currentLocation,userLocations);
结果应该是一个具有最近位置的数组。我怎样才能做到这一点?提前致谢:)
如果不获取整个数据集并在本地处理,就无法索引和查询 Firebase 实时数据库。
我建议改用新发布的测试版 Cloud Firestore,它甚至将地理点作为数据类型,并且更好地支持 sql-like "where queries"。
Cloud Firestore queries
GeoFire 是根据与特定点的接近程度查询 Firebase 实时数据库的唯一方法,无需下载所有数据和过滤客户端。
有了 GeoFire,你 create a GeoQuery
for a location and a maximum distance:
var geoQuery = geoFire.query({
center: [10.38, 2.41],
radius: 10.5
});
然后你 attach handlers for items that fall within this range:
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});
没有用于查找单个最接近项目的内置功能。您可以根据回调中的 distance
参数在客户端执行该操作。您仍然会检索比需要更多的数据,但希望它会少于整个数据库。
我正在用一个例子来解释以实现我的结果。
我有一个用户table。每个用户在数据库中都有他们的位置和经纬度。我正在使用 Firebase 数据库。所以数据库看起来像:
users {
user-id{
location{
latitude
longitude
}
}
}
我需要使用我当前的位置在一个圈子内找到用户。
我调查了 geolib and found a method isPointInCircle 。但是使用这个,我们需要迭代用户数据并且需要在每个循环中调用这个方法。我需要避免这种多次调用并仅通过一个方法调用来实现,例如:
findNearestUsers(currentLocation,userLocations);
结果应该是一个具有最近位置的数组。我怎样才能做到这一点?提前致谢:)
如果不获取整个数据集并在本地处理,就无法索引和查询 Firebase 实时数据库。
我建议改用新发布的测试版 Cloud Firestore,它甚至将地理点作为数据类型,并且更好地支持 sql-like "where queries"。
Cloud Firestore queries
GeoFire 是根据与特定点的接近程度查询 Firebase 实时数据库的唯一方法,无需下载所有数据和过滤客户端。
有了 GeoFire,你 create a GeoQuery
for a location and a maximum distance:
var geoQuery = geoFire.query({
center: [10.38, 2.41],
radius: 10.5
});
然后你 attach handlers for items that fall within this range:
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});
没有用于查找单个最接近项目的内置功能。您可以根据回调中的 distance
参数在客户端执行该操作。您仍然会检索比需要更多的数据,但希望它会少于整个数据库。