0
votes

I have been developing an android application for a gas station company. The application uses google map.

The user selects two points on the map and i show the route between selected points. The application needs to show nearest(in 2 miles radius) gas stations on the selected route.

The route is a coordinate array like: Route[5000] = {"lon1,lat1","lon2,lat2","lon3,lat3","lon4,lat5"... }

And i have a gas stations list which consists of coordinates. GasStations[200] = {"lon1,lat1","lon2,lat2","lon3,lat3","lon4,lat5"... }

Can you suggest me a high performance algorithm for calculating the nearest gas stations on the route.

Thanks.

1

1 Answers

1
votes

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));
}