我正在使用 Grails 2.1.1,并且必须找到给定点的所有最近位置。我的代码看起来像这样(在我的应用程序中工作):
def findNearLocations(double latitude, double longitude) {
def locationsFromQuery = Location.executeQuery(
"SELECT id,(6371 * 2 * ASIN(SQRT(POWER(SIN((:ulatitude - abs(latitude)) * pi()/180 / 2),2) +" +
"COS(:ulatitude * pi()/180 ) * COS(abs(latitude) * pi()/180) *" +
"POWER(SIN((:ulongitude - longitude) * pi()/180 / 2), 2))))*1000 as distance " +
"FROM Location WHERE is_public = TRUE ORDER BY distance", // HAVING distance < 1000
[ulatitude: latitude.toString().toDouble(), ulongitude: longitude.toString().toDouble()])
// do stuff with locationsFromQuery
}
我用于计算最近位置的公式是这个堆栈溢出帖子,但我不能像评论中那样使用 HAVING 子句。如果我使用它,我会得到一个 hql.hibernate 异常,说有一个意外的标识符 HAVING。但是为什么这在谷歌的几乎所有示例中都有效,但对我无效?
我在内存中使用 h2 数据库。