I use c# code to calculate the distance. I enumerate all of the data in the database and if the data is in range of my distance I add it to the array and after that I pass it to the device. In my code I provide lat and lon for user current position and lat and lon for object from database. I also provide unit K - km and M - miles. This is a c# code but you can easy convert it to java:
public double GetDistanceFromLatLong(double lat1, double lon1, double lat2, double lon2, string unit)
{
double ReturnValue = 0;
double theta = 0;
double dist = 0;
theta = lon1 - lon2;
dist = Math.Sin(DegreesToRadians(lat1)) * Math.Sin(DegreesToRadians(lat2)) + Math.Cos(DegreesToRadians(lat1)) *
Math.Cos(DegreesToRadians(lat2)) * Math.Cos(DegreesToRadians(theta));
dist = Math.Acos(dist);
dist = RadiansToDegrees(dist);
ReturnValue = dist * 60 * 1.1515;
switch (unit.ToUpper())
{
case "K":
ReturnValue = ReturnValue * 1.609344;
break;
case "M":
ReturnValue = ReturnValue * 0.8684;
break;
}
return ReturnValue;
}