我在我的应用程序中使用 objectify-appengine。在数据库中,我存储地点的纬度和经度。 在某些时候,我想找到离特定点最近的地方(从数据库)。
据我所知,我无法执行常规的类似 SQL 的查询。 所以我的问题是如何以最好的方式完成它?

请您参考如下方法:

你应该看看GeoModel ,它支持使用 Google App Engine 进行地理空间查询。

更新:

假设您在 Objectify 注释模型类中有一个名为 coordinates 的 GeoPt 属性。

您的项目中需要有两个库:

  1. > GeoLocation.java
  2. > Java GeoModel

在要执行地理查询的代码中,您具有以下内容:

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; 
    } 
} 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!