Skip to content

Instantly share code, notes, and snippets.

@javieritis
Last active December 19, 2019 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javieritis/6e92331b9eecfde0be7f427dc6b84946 to your computer and use it in GitHub Desktop.
Save javieritis/6e92331b9eecfde0be7f427dc6b84946 to your computer and use it in GitHub Desktop.
Find points in radius distance.
/**
* https://clickherelabs.com/development-2/distance-searching-and-mysql/
* http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates
*/
static int distance = 10;
static double origLatitude = 0; //your latitude in degrees
static double origLongitude = 0; //your longitude in degrees
public static void main(String[] args) {
List<LatLng> result = filterLocations(origLatitude, origLongitude, distance, getList());
if (result.size() > 0) {
for (LatLng latLng : result) {
System.out.println(latLng.getNombre() + " " + latLng.getDistancia() + "m");
}
} else {
System.out.println("Empty.");
}
}
private static List<LatLng> filterLocations(double origLatitude, double origLongitude, int distance, List<LatLng> points) {
List<LatLng> pointsFiltered = new ArrayList<>();
for (LatLng item : points) {
double variable = distance / (69 * Math.cos(Math.toRadians(origLatitude)));
double value = (double) distance / 69;
if ((item.getLatitude() >= origLatitude - value && item.getLatitude() <= origLatitude + value) &&
(item.getLongitude() >= origLongitude - (variable) && item.getLongitude() <= origLongitude + variable)) {
double distanceSite = 3956 * Math.acos(Math.cos(Math.toRadians(origLatitude)) * Math.cos(Math.toRadians(item.getLatitude())) * Math.cos(Math.toRadians(origLongitude) - Math.toRadians(item.getLongitude())) + Math.sin(Math.toRadians(origLatitude)) * Math.sin(Math.toRadians(item.getLatitude())));
if (distanceSite <= distance) {
item.setDistancia(distanceSite);
pointsFiltered.add(item);
}
}
}
return pointsFiltered;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment