问题
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:
- 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;
}
}
来源:https://stackoverflow.com/questions/13671699/how-to-find-the-closest-point-in-the-db-using-objectify-appengine