如何使用objectify-appengine在数据库中找到最近的点 [英] How to find the closest point in the DB using objectify-appengine
问题描述
我在我的应用程序中使用了objectify-appengine。在DB中,我存储了纬度&地方的经度。
在某些时候,我想找到最接近的地方(从数据库)到特定点。
据我所知,我不能执行常规的类似SQL的查询。
所以我的问题是如何以最好的方式完成?
I'm using objectify-appengine in my app. In the DB I store latitude & longitude of places.
at some point I'd like to find the closest place (from the DB) to a specific point.
As far as i understood i can't perform regular SQL-like queries.
So my question is how can it be done in the best way?
推荐答案
你应该看看< href =https://developers.google.com/maps/articles/geospatial> GeoModel ,它可以使用Google App Engine启用地理空间查询。
You should take a look at GeoModel, which enables Geospatial Queries with Google App Engine.
更新:
假设您在Objectify注释模型类中有一个GeoPt属性,名为 coordinates
。
Let's assume that you have in your Objectify annotated model class, a GeoPt property called coordinates
.
您需要在您的项目中拥有两个库:
You need to have in your project two libraries:
- GeoLocation.java
- Java GeoModel
在您想要执行地理查询的代码中,您有以下内容:
In the code that you want to perform a geo query, you have the following:
import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.model.BoundingBox;
import you.package.path.GeoLocation;
// other imports
// in your method
GeoLocation specificPointLocation = GeoLocation.fromDegrees(specificPoint.latitude, specificPoint.longitude);
GeoLocation[] bc = specificPointLocation.boundingCoordinates(radius);
// Transform this to a bounding box
BoundingBox bb = new BoundingBox((float) bc[0].getLatitudeInDegrees(),
(float) bc[1].getLongitudeInDegrees(),
(float) bc[1].getLatitudeInDegrees(),
(float) bc[0].getLongitudeInDegrees());
// Calculate the geocells list to be used in the queries (optimize
// list of cells that complete the given bounding box)
List<String> cells = GeocellManager.bestBboxSearchCells(bb, null);
// calculate geocells of your model class instance
List <String> modelCells = GeocellManager.generateGeoCell(myInstance.getCoordinate);
// matching
for (String c : cells) {
if (modelCells.contains(c)) {
// success, do sth with it
break;
}
}
这篇关于如何使用objectify-appengine在数据库中找到最近的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!