public void proximityToEndPoint(String username, String password,
double latitude, double longitude) {
double radius = 6371;
connect2DB();
if (this.conn != null) {
try {
String query = "";
PreparedStatement statement;
@SuppressWarnings("unused")
ResultSet resultSet;
double lat1 = 33.420243;
double long1 = -111.936125;
double lat2 = 33.3070;
double long2 = 111.6783;
query = "SELECT username, password"
+ "FROM endpoint, rs_user\n" + "WHERE lat AND long IN "
+ calculateDistance(lat1, long1, lat2, long2);
statement = this.conn.prepareStatement(query);
statement.setString(1, username);
statement.setString(2, password);
resultSet = statement.executeQuery();
this.conn.commit();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
closeDBConnection();
}
public static double calculateDistance(double lat1, double lng1,
double lat2, double lng2) {
int r = 6371; // average radius of the earth in km
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = r * c;
double e = 0.621371 * d;
return e;
}
How do you determine whether a user in a certain radius of a location (represented by a set of coordinates in latitude and longitude)? If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Way to decrease size of apk file | 9 | 69 | |
How Can I resolve this Error "java.sql.SQLException: Invalid column index" | 4 | 38 | |
Error in @AspectJ Based AOP with Spring | 2 | 13 | |
eclipse console opening separately | 2 | 21 |
Join the community of 500,000 technology professionals and ask your questions.