GeoFire 结果返回未按距离排序
GeoFire Results Coming Back Unordered By Distance
我正在使用:
geofire#4.1.2 bower_components\geofire
└── firebase#3.9.0
我将此函数写到 return 基于用户接近度的地标列表:
GetAllLandmarksByUserLocation(ref, ref_locations, properties, user_latitude, user_longitude,
user_radius)
{
const landmarkGeoFire = new GeoFire(ref_locations);
var geoQuery = landmarkGeoFire.query({
center: [user_latitude, user_longitude],
radius: user_radius
});
return new Promise(function(resolve,reject)
{
var locations = [];
var onKeyEnteredRegistration = geoQuery.on("key_entered",
function (key, coordinates, distance) {
var location = {};
location.key = key;
location.latitude = coordinates[0];
location.longitude = coordinates[1];
location.distance = distance;
locations.push(location);
});
var attributes = [];
var onReadyRegistration = geoQuery.on("ready", function() {
ref.on('value', function (refsSnap) {
refsSnap.forEach((refSnap) => {
var list = refSnap;
locations.forEach(function(locationSnap)
{
//console.log(refSnap.key, '==', locationSnap.key);
// brute force approach, rework this later
if (refSnap.key == locationSnap.key)
{
var attribute = {};
for(var i=0; i<=properties.length-1; i++)
{
if(properties[i] == 'key') {
attribute[properties[i]] = refSnap.key;
continue;
}
attribute[properties[i]] = list.child(properties[i]).val();
}
attribute['latitude'] = locationSnap.latitude;
attribute['longitude'] = locationSnap.longitude;
attribute['distance'] = locationSnap.distance;
attributes.push(attribute);
} // refSnap.key == locationSnap.key
}); // locations.forEach
}); // refsSnap.forEach
resolve(attributes);
}); // ref.on
}); // onreadyregistration
}); // Promise
}
但是,返回的列表并不是按距离排序的。我发现这特别奇怪,因为过去返回的结果是按距离排序的。从那以后我做过的事情:
- 添加了 Swagger
- 更改了项目结构以符合 OpenAPI 规范。
- 添加凉亭
- 重新安装了 geofire 和 firebase
所以,我以前知道这个代码"has worked"。
这是我的 Firebase RTDB 结构:
对于输入:
user_latitude = 40.769841
user_longitude = -73.964306
user_radius = 225
我现在得到以下结果:
[
{
"key": "-L8AURBnFy01EZVekPnR",
"building": "753",
"street": "York Avenue",
"category": 0,
"closing": "6 PM",
"email": "cornell@somewhere.com",
"name": "Weill Cornell",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:15:55 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.weillcornell.com",
"latitude": 40.765982,
"longitude": -73.956045,
"distance": 0.817387865219758
},
{
"key": "-L8AUdrIysiTYr0RUDV_",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "cornell@somewhere.com",
"name": "Weill Cornell",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:16:51 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.weillcornell.com",
"latitude": 40.765982,
"longitude": -73.956045,
"distance": 0.817387865219758
},
{
"key": "-L8AUeEayBJKU3y6Hd4e",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "cornell@somewhere.com",
"name": "Weill Cornell",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:16:53 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.weillcornell.com",
"latitude": 40.765982,
"longitude": -73.956045,
"distance": 0.817387865219758
},
{
"key": "-L8AVW50fx3cTwn-DrIQ",
"building": "753",
"street": "Minetta Lane",
"category": 0,
"closing": "6 PM",
"email": "support@southebys.com",
"name": "Southebys",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:20:38 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.southebys.com",
"latitude": 40.766372,
"longitude": -73.953803,
"distance": 0.9649556738877696
},
{
"key": "-L8AV_nVSAzMxdAXJb-L",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@southebys.com",
"name": "Southebys",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:20:57 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.southebys.com",
"latitude": 40.766372,
"longitude": -73.953803,
"distance": 0.9649556738877696
},
{
"key": "-L8AWqfH3q4dZcrFMc6F",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@helmsley.com",
"name": "helmsley",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:26:28 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.helmsley.com",
"latitude": 40.76573,
"longitude": -73.954242,
"distance": 0.9629547665546629
},
{
"key": "-L8AX3omgzPs87WMYTq6",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@tanoshisushi.com",
"name": "tanoshisushi",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:27:26 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.tanoshisushi.com",
"latitude": 40.76759,
"longitude": -73.952848,
"distance": 0.9968555619790017
},
{
"key": "-L8AXK4nyV64H7iI0Amf",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@tanoshisushi.com",
"name": "tanoshisushi",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:28:33 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.tanoshisushi.com",
"latitude": 40.768509,
"longitude": -73.955197,
"distance": 0.7812649215069556
},
{
"key": "-L8AXOgp3hN8QylH7LO9",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@bearburger.com",
"name": "bearburger",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:28:52 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.bearburger.com",
"latitude": 40.768509,
"longitude": -73.955197,
"distance": 0.7812649215069556
},
{
"key": "-L8AXcbWrfZhEUR3y8uo",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@townschool.com",
"name": "townschool",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:29:53 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.townschool.com",
"latitude": 40.768435,
"longitude": -73.949715,
"distance": 1.2386598393876396
},
{
"key": "-L8AY5SWt184Xk2IUl2B",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@asiasociety.com",
"name": "asiasociety",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:31:55 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.asiasociety.com",
"latitude": 40.769841,
"longitude": -73.964306,
"distance": 0
},
{
"key": "-L8AYbyXQIbriYHk5PbC",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@jgmellon.com",
"name": "jgmellon",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:34:12 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.jgmellon.com",
"latitude": 40.77106,
"longitude": -73.959339,
"distance": 0.4396927136586955
},
{
"key": "-L8AZaR3uQ6Q5k_ZIC80",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@jgmellon.com",
"name": "jgmellon",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:38:28 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.jgmellon.com",
"latitude": 40.77106,
"longitude": -73.959339,
"distance": 0.4396927136586955
},
{
"key": "-L8D5oZsmcoMLdLR9ird",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@sojourn.com",
"name": "sojourn",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Thu Mar 22 2018 10:27:13 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.sojourn.com",
"latitude": 40.773449,
"longitude": -73.955691,
"distance": 0.8290110965288692
},
{
"key": "-L8TSMb95TLDZJv78CGY",
"building": "19",
"street": "Park Avenue",
"category": 0,
"closing": "6 PM",
"email": "support@williams.com",
"name": "williams",
"opening": "8 AM",
"phone": "203-453-4737",
"postal": 10932,
"timestamp": "Sun Mar 25 2018 14:39:39 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.williams.com",
"latitude": 41.288906,
"longitude": -72.674364,
"distance": 122.63288097507322
}
]
非常感谢您的反馈。
GeoFire 按距离过滤结果,但不会 return 按距离递增的顺序过滤结果。如果需要,您需要在应用程序代码中捕获事件,并在那里对它们进行排序。
请注意,这不是最近的更改:GeoFire 从未按距离顺序 returned 结果。给定基础数据库和查询模型,这将无法很好地扩展。
我正在使用:
geofire#4.1.2 bower_components\geofire
└── firebase#3.9.0
我将此函数写到 return 基于用户接近度的地标列表:
GetAllLandmarksByUserLocation(ref, ref_locations, properties, user_latitude, user_longitude,
user_radius)
{
const landmarkGeoFire = new GeoFire(ref_locations);
var geoQuery = landmarkGeoFire.query({
center: [user_latitude, user_longitude],
radius: user_radius
});
return new Promise(function(resolve,reject)
{
var locations = [];
var onKeyEnteredRegistration = geoQuery.on("key_entered",
function (key, coordinates, distance) {
var location = {};
location.key = key;
location.latitude = coordinates[0];
location.longitude = coordinates[1];
location.distance = distance;
locations.push(location);
});
var attributes = [];
var onReadyRegistration = geoQuery.on("ready", function() {
ref.on('value', function (refsSnap) {
refsSnap.forEach((refSnap) => {
var list = refSnap;
locations.forEach(function(locationSnap)
{
//console.log(refSnap.key, '==', locationSnap.key);
// brute force approach, rework this later
if (refSnap.key == locationSnap.key)
{
var attribute = {};
for(var i=0; i<=properties.length-1; i++)
{
if(properties[i] == 'key') {
attribute[properties[i]] = refSnap.key;
continue;
}
attribute[properties[i]] = list.child(properties[i]).val();
}
attribute['latitude'] = locationSnap.latitude;
attribute['longitude'] = locationSnap.longitude;
attribute['distance'] = locationSnap.distance;
attributes.push(attribute);
} // refSnap.key == locationSnap.key
}); // locations.forEach
}); // refsSnap.forEach
resolve(attributes);
}); // ref.on
}); // onreadyregistration
}); // Promise
}
但是,返回的列表并不是按距离排序的。我发现这特别奇怪,因为过去返回的结果是按距离排序的。从那以后我做过的事情:
- 添加了 Swagger
- 更改了项目结构以符合 OpenAPI 规范。
- 添加凉亭
- 重新安装了 geofire 和 firebase
所以,我以前知道这个代码"has worked"。
这是我的 Firebase RTDB 结构:
对于输入:
user_latitude = 40.769841
user_longitude = -73.964306
user_radius = 225
我现在得到以下结果:
[
{
"key": "-L8AURBnFy01EZVekPnR",
"building": "753",
"street": "York Avenue",
"category": 0,
"closing": "6 PM",
"email": "cornell@somewhere.com",
"name": "Weill Cornell",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:15:55 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.weillcornell.com",
"latitude": 40.765982,
"longitude": -73.956045,
"distance": 0.817387865219758
},
{
"key": "-L8AUdrIysiTYr0RUDV_",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "cornell@somewhere.com",
"name": "Weill Cornell",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:16:51 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.weillcornell.com",
"latitude": 40.765982,
"longitude": -73.956045,
"distance": 0.817387865219758
},
{
"key": "-L8AUeEayBJKU3y6Hd4e",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "cornell@somewhere.com",
"name": "Weill Cornell",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:16:53 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.weillcornell.com",
"latitude": 40.765982,
"longitude": -73.956045,
"distance": 0.817387865219758
},
{
"key": "-L8AVW50fx3cTwn-DrIQ",
"building": "753",
"street": "Minetta Lane",
"category": 0,
"closing": "6 PM",
"email": "support@southebys.com",
"name": "Southebys",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:20:38 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.southebys.com",
"latitude": 40.766372,
"longitude": -73.953803,
"distance": 0.9649556738877696
},
{
"key": "-L8AV_nVSAzMxdAXJb-L",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@southebys.com",
"name": "Southebys",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:20:57 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.southebys.com",
"latitude": 40.766372,
"longitude": -73.953803,
"distance": 0.9649556738877696
},
{
"key": "-L8AWqfH3q4dZcrFMc6F",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@helmsley.com",
"name": "helmsley",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:26:28 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.helmsley.com",
"latitude": 40.76573,
"longitude": -73.954242,
"distance": 0.9629547665546629
},
{
"key": "-L8AX3omgzPs87WMYTq6",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@tanoshisushi.com",
"name": "tanoshisushi",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:27:26 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.tanoshisushi.com",
"latitude": 40.76759,
"longitude": -73.952848,
"distance": 0.9968555619790017
},
{
"key": "-L8AXK4nyV64H7iI0Amf",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@tanoshisushi.com",
"name": "tanoshisushi",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:28:33 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.tanoshisushi.com",
"latitude": 40.768509,
"longitude": -73.955197,
"distance": 0.7812649215069556
},
{
"key": "-L8AXOgp3hN8QylH7LO9",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@bearburger.com",
"name": "bearburger",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:28:52 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.bearburger.com",
"latitude": 40.768509,
"longitude": -73.955197,
"distance": 0.7812649215069556
},
{
"key": "-L8AXcbWrfZhEUR3y8uo",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@townschool.com",
"name": "townschool",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:29:53 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.townschool.com",
"latitude": 40.768435,
"longitude": -73.949715,
"distance": 1.2386598393876396
},
{
"key": "-L8AY5SWt184Xk2IUl2B",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@asiasociety.com",
"name": "asiasociety",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:31:55 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.asiasociety.com",
"latitude": 40.769841,
"longitude": -73.964306,
"distance": 0
},
{
"key": "-L8AYbyXQIbriYHk5PbC",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@jgmellon.com",
"name": "jgmellon",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:34:12 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.jgmellon.com",
"latitude": 40.77106,
"longitude": -73.959339,
"distance": 0.4396927136586955
},
{
"key": "-L8AZaR3uQ6Q5k_ZIC80",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@jgmellon.com",
"name": "jgmellon",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Wed Mar 21 2018 22:38:28 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.jgmellon.com",
"latitude": 40.77106,
"longitude": -73.959339,
"distance": 0.4396927136586955
},
{
"key": "-L8D5oZsmcoMLdLR9ird",
"building": "753",
"street": null,
"category": 0,
"closing": "6 PM",
"email": "support@sojourn.com",
"name": "sojourn",
"opening": "8 AM",
"phone": "212-555-1212",
"postal": 10021,
"timestamp": "Thu Mar 22 2018 10:27:13 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.sojourn.com",
"latitude": 40.773449,
"longitude": -73.955691,
"distance": 0.8290110965288692
},
{
"key": "-L8TSMb95TLDZJv78CGY",
"building": "19",
"street": "Park Avenue",
"category": 0,
"closing": "6 PM",
"email": "support@williams.com",
"name": "williams",
"opening": "8 AM",
"phone": "203-453-4737",
"postal": 10932,
"timestamp": "Sun Mar 25 2018 14:39:39 GMT-0400 (Eastern Daylight Time)",
"type": 4,
"web": "http://www.williams.com",
"latitude": 41.288906,
"longitude": -72.674364,
"distance": 122.63288097507322
}
]
非常感谢您的反馈。
GeoFire 按距离过滤结果,但不会 return 按距离递增的顺序过滤结果。如果需要,您需要在应用程序代码中捕获事件,并在那里对它们进行排序。
请注意,这不是最近的更改:GeoFire 从未按距离顺序 returned 结果。给定基础数据库和查询模型,这将无法很好地扩展。