--- android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/05/18 17:56:43 733 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/StationDAO.java 2010/05/19 09:53:25 739 @@ -101,15 +101,15 @@ return result; } + //the "hack" with max 1.5 degrees latitude and 2.5 degrees longitude is only valid since we only service danish trains + // in denmark 1.5dg latitude ~ 165km, 2.5dg longitude ~ 155km public List getByLocation(double latitude, double longitude) throws SQLException { - String SQL = "SELECT * FROM ( "+ - " SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + - " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + - " FROM trainstations " + - " WHERE enabled = true " + - " ) AS trainstations2 " + - "ORDER BY calcdist ASC " + - "LIMIT 4 "; + String SQL = "SELECT id,name,latitude,longitude,stationcode_fjrn,stationcode_stog, stationcode_metro, address," + + " earth_distance( earth_coord, ll_to_earth(?,?))::int AS calcdist " + + "FROM trainstations " + + "WHERE enabled = true AND abs(latitude-?)<1.5 AND abs(longitude-?)<2.5 " + + "ORDER BY calcdist ASC " + + "LIMIT 4 "; List result; Connection conn = null; PreparedStatement stmt = null; @@ -119,6 +119,8 @@ stmt = conn.prepareStatement(SQL); stmt.setDouble(1, latitude); stmt.setDouble(2, longitude); + stmt.setDouble(3, latitude); + stmt.setDouble(4, longitude); res = stmt.executeQuery(); result = convertResultset(res);