I am not familiar with android. Here the C# method to calculate the distance between two pairs of location. If distance is within 2 miles radius, pick that station.
Yes, you will have to loop through 5000x200 = 1,000,000, but it is quite fast.
I hope that will help.
decimal distance = CalculateDistance(stationLatitude, stationLongitude, routeLatitude, routeLongitude);
private static double ToRadian(double val)
{
return (Math.PI / 180) * val;
}
private static double ToXAxis(decimal lat, decimal lng)
{
return (Math.Cos(4 * (4 * Math.Atan2(1, 5) - Math.Atan2(1, 239)) / 180 * (double)lat) *
Math.Cos(4 * (4 * Math.Atan2(1, 5) - Math.Atan2(1, 239)) / 180 * (double)lng));
}
private static double ToYAxis(decimal lat, decimal lng)
{
return (Math.Cos(4 * (4 * Math.Atan2(1, 5) - Math.Atan2(1, 239)) / 180 * (double)lat) *
Math.Sin(4 * (4 * Math.Atan2(1, 5) - Math.Atan2(1, 239)) / 180 * (double)lng));
}
private static double ToZAxis(decimal lat)
{
return Math.Sin(4 * (4 * Math.Atan2(1, 5) - Math.Atan2(1, 239)) / 180 * (double)lat);
}
private static decimal CalculateDistance(decimal lat1, decimal lng1, decimal lat2, decimal lng2)
{
double cntXAxis = Math.Cos(ToRadian((double) lat1))*Math.Cos(ToRadian((double) lng1));
double cntYAxis = Math.Cos(ToRadian((double) lat1))*Math.Sin(ToRadian((double) lng1));
double cntZAxis = Math.Sin(ToRadian((double) lat1));
return (decimal) (3961*Math.Acos(ToXAxis(lat2, lng2)*cntXAxis + ToYAxis(lat2, lng2)*cntYAxis + ToZAxis(lat2)*cntZAxis));
}