(请查看截至最后的截图更新)我需要查询当前位置20KM / 50KM范围内的酒店表,我想使用边界框过滤掉不在该范围内的所有酒店,所以下面是我的边界框方法:
public static double[] calculateBoundingBox(double aLat, double aLon, double distance, int units) {
double radius = 6378.1; // kilometer
// bearings
double dueNorth = 0.0;
double dueSouth = 180.0;
double dueEast = 90.0;
double dueWest = 270.0;
double aLatRad = aLat / DEGREES_TO_RADIANS;
double aLonRad = aLon / DEGREES_TO_RADIANS;
double northMost = Math.asin(Math.sin(aLatRad) * Math.cos(distance / radius) + Math.cos(aLatRad) * Math.sin(distance / radius) * Math.cos(dueNorth));
double southMost = Math.asin(Math.sin(aLatRad) * Math.cos(distance / radius) + Math.cos(aLatRad) * Math.sin(distance / radius) * Math.cos(dueSouth));
double eastMost = aLonRad + Math.atan2(Math.sin(dueEast)*Math.sin(distance / radius)*Math.cos(aLatRad),Math.cos(distance / radius)-Math.sin(aLatRad)*Math.sin(aLatRad));
double westMost = aLonRad + Math.atan2(Math.sin(dueWest)*Math.sin(distance / radius)*Math.cos(aLatRad),Math.cos(distance / radius)-Math.sin(aLatRad)*Math.sin(aLatRad));
northMost = Math.toDegrees(northMost);
southMost = Math.toDegrees(southMost);
eastMost = Math.toDegrees(eastMost);
westMost = Math.toDegrees(westMost);
double lat1 = 0.0, lat2 = 0.0;
double lon1 = 0.0, lon2 = 0.0;
// sort latitude and longitude for "BETWEEN" query
if (northMost > southMost) {
lat1 = southMost;
lat2 = northMost;
} else {
lat1 = northMost;
lat2 = southMost;
}
if (eastMost > westMost) {
lon1 = westMost;
lon2 = eastMost;
} else {
lon1 = eastMost;
lon2 = westMost;
}
Log.d("GEO", "Bounding Box: " + lat1 + "," + lon1 + " " + lat2 + "," + lon2);
return new double[]{lat1, lat2, lon1, lon2};
}
和SQL where子句:
String selection = "(" + FIELD_NAME_HOTEL_POS_LATITUDE + " BETWEEN " + boundingBox[0] + " AND " + boundingBox[1] + ") AND ("
+ FIELD_NAME_HOTEL_POS_LONGITUDE + " BETWEEN " + boundingBox[2] + " AND " + boundingBox[3] + ")";
但结果是在指定的20KM / 50KM范围内找到的酒店较少,我找不到任何问题,有人可以查看我的代码并指出我正确的方向吗?
非常感谢!
编辑:
我发现了问题,下面是计算边界框的伪图像:
geomap http://i53.tinypic.com/25ui6xl.png
指出了四个角和我的位置,但我无法弄清楚我的方法的问题。