I have an Array of longitude and latitude of landmarks. How do i calculate the distance between the landmark and the user's location to get the landmarks which are for example in a 10km radius from the user's location
private static double distanceInKm(double lat1, double lon1, double lat2, double lon2) {
int R = 6371; // km
double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
double y = (lat2 - lat1);
return (Math.sqrt(x * x + y * y) * R) / 1000;
}
Or
Location location1 = new Location("");
location1.setLatitude(latitude1);
location1.setLongitude(longitude1);
Location location2 = new Location("");
location2.setLatitude(latitude2);
location2.setLongitude(longitude2);
float distanceInKm = (location1.distanceTo(location2))/1000;