Firestore双字段查询请求(以及GeoPoint) [英] Firestore double field query request (and with GeoPoint)

查看:50
本文介绍了Firestore双字段查询请求(以及GeoPoint)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收集了两个字段的文档:位置(Geopoint)和距离(Number).用户输入的是另一个位置(Geopoint).

I have got a collection of documents with two fields: a location (Geopoint) and a distance (Number). The user input is another location (Geopoint).

我必须返回文档位置和输入位置之间的距离小于文档距离的所有文档.

I've to return all the documents where the distance between the location of the document and the location of the input is less than the distance of the document.

教程显示了如何在附近表演位置查询,但是如果用户未给出距离但每个文档都不同,该如何在距离中插入距离?

This tutorial shows how to perform nearby location queries but how can I insert in the equation the distance if it is not given by the user but different for each document?

推荐答案

我想出了一个解决方案.

I came up with a solution.

我可以存储外接正方形,而不是保存一个地理点和一个半径(表示一个圆),所以我将存储两个经度(geopoint经度+和-半径)和两个纬度(geopoint纬度+和-半径).然后,我可以进行4部分的复合查询,以查看用户地理位置是否在正方形中.

Instead of saving a geopoint and a radius (representing a circle), I can store the circumscribed square, so I will store two longitudes (geopoint longitude + and - radius) and two latitudes (geopoint latitude + and - radius). Then I can do a compound query of 4 parts to see if the user geopoint is in the square.

无论如何,这不是一个完美的解决方案,因为圆/正方形近似值缺乏精确度,但是在我的特定情况下,这并不重要.因此,如果您的精度很重要,则可以保存半径(或从平方宽计算),然后对查询的对象进行过滤.

Anyway, this is an imperfect solution because there is a lack of precision in the circle/square approximation, but in my specific case, it doesn't matter. Therefore if in your case precision matters you can save radius (or calculate from square wide), and then make a filter on the objects you queried.

泛化,该方法可以轻松地应用于任何多边形:

Generalizing, this method can be easily applied to any polygon:

  1. 将多边形坐标和外接矩形保存在Firestore文档中.
  2. 使用四个矩形坐标在Firestore中查询文档
  3. 使用原始多边形过滤结果.

这篇关于Firestore双字段查询请求(以及GeoPoint)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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