irldev
irldev

Reputation: 409

How to determine nearest co-ordinate

I am working on a Windows Phone 8 app and I am doing a location based search. I am using this example to find my current location which is working fine. I also have a list of latitude and longitude co-ordinates. What I want to do is find out from this list of latitude and longitude co-ordinates, which is the closest to my current location. I may also possibly modify this to find out the nearest 5 or 10 or something like that.

It sounds like it should be simple to find out but I don't know how to do it.

How do I find out which co-ordinates are closest to another?

Any help would be appreciated.

Thanks!

Upvotes: 0

Views: 886

Answers (3)

david strachan
david strachan

Reputation: 7228

The radius of the earth at equator = 6,371 km. The equator is divided into 360 degrees of longitude, so each degree at the equator represents approximately 111.32 km. Moving away from the equator towards a pole this distance decreases to zero at the pole.To calculate the distance at different latitudes multiply it by the cosine of the latitude

3 decimal places,0.001 degrees aproximates to 111.32 meters at equator
96.41meters at 30 degrees N/S
78.71 meters at 45 degrees N/S
55.66 meters at 60 degrees N/S
28.82 meters at 75 degrees N/S

For for small distances (100 meters) Pythagoras’ theorem can be used on an equirectangular projection to calculate distance. This is less complicated than Haversine or Spherical Law of Cosines but still allows for the convergence towards poles.

var R = 6371; // km
lat/lng in radians

In pseudo code as I don't know C#

var x = (lng2-lng1) * cos((lat1+lat2)/2);
var y = (lat2-lat1);
var d = sqrt(x*x + y*y) * R;

Upvotes: 0

Andrew - OpenGeoCode
Andrew - OpenGeoCode

Reputation: 2287

Since your distances are probably very short (e.g., < 25km) you can use a distance approximation vs. the Haversine formula. I would suggest using Pythagoras Theorem on an Equirectangular projection, which will correct for the curvature along the longitude lines. Below is a C# implementation:

// Convert Degress to Radians
//
private static double Deg2Rad( double deg ) 
{
   return deg * Math.PI / 180;
}

// Get Distance between two lat/lng points using the PythagorsTheorm (a*a = (b*b + c*c))
// on an equirectangular projection
//
private double PythagorasEquirectangular( Geoposition coord1, Geoposition coord2 )
{
    double lat1 = Deg2Rad( coord1.Coordinate.Latitude );
    double lat2 = Deg2Rad( coord2.Coordinate.Latitude );
    double lon1 = Deg2Rad( coord1.Coordinate.Longitude );
    double lon2 = Deg2Rad( coord2.Coordinate.Longitude );

    double R = 6371; // km
    double x = (lon2-lon1) * Math.Cos((lat1+lat2)/2);
    double y = (lat2-lat1);
    double d= Math.Sqrt(x*x + y*y) * R;
    return d;
}

// Find the closest point to your position
//
private Geoposition NearestPoint( List<Geoposition> points, Geoposition position  )
{
    double min_dist = 999999;
    Geoposition closest = null;

    // Calculate distance of each point in the list from your position
    foreach ( Geoposition point in points )
    {
        double dist = PythagorasEquirectangular( position, point );

        // keep track of which point is the current closest.
        if ( dist < min_dist )
        {
            min_dist = dist;
            closest = point;
        }
    }

    // return the closest point
    return closest;
}

Upvotes: 0

pid
pid

Reputation: 11597

The actual distance requires a geodesic function:

http://en.wikipedia.org/wiki/Great-circle_distance

It is quite costly, so you want to filter first with another function that helps you trimming down the candidates and ordering them later on in your code.

In this prior pass you can use the euclidean distance:

http://en.wikipedia.org/wiki/Euclidean_distance

This two-pass approach greatly reduces computation costs (by factors of 10,000 if need be) and is described in Programming Pearls (chapter 8):

http://www.cs.bell-labs.com/cm/cs/pearls/

Upvotes: 1

Related Questions