How to find the closest point in the DB using objectify-appengine

拥有回忆 提交于 2020-01-15 07:20:06

问题


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?


回答1:


You should take a look at GeoModel, which enables Geospatial Queries with Google App Engine.

Update:

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:

  1. GeoLocation.java
  2. 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;
    }
}


来源:https://stackoverflow.com/questions/13671699/how-to-find-the-closest-point-in-the-db-using-objectify-appengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!