简体   繁体   中英

C# Finding which geometry value from a List is closest to a static geometry point

I have created my own class 'Geolocation' which just holds double variables 'longitude' and 'latitude'.

I have a List and another static Geolocation object. I need to loop through my List and analyse which of these locations is closest to the static location. By 'closest' I am referring to real life distance, so I think it would be called a geospatial analysis.

My own pseudo idea is along the lines of this.

Geolocation[] closestPositions = new Geolocation[]

for (int i = 0; i < TrainStations.AllStations.Count; i++)
{
    Geolocation A = TrainStations.AllStations[i];
    Geolocation B = DataHandler.UserCurrentPosition;

    if (A and B are closer than what is stored in closestPosition)
    {
       closestPosition[0] = A;
       closestPosition[1] = B;
    }
}

By the end of this loop I would be left with the two closest points (more specifically which train station in the city is closest to the user's current position).

However, I'm really not sure if the way my pesudo code would function is most efficient, and I don't know how to do the actual analysis (get the distances from A to B and measure which is shortest).

Rather than creating my own class 'Geolocation', I should have used the inbuilt System.Device.Location.GeoCoordinate class.

https://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate(v=vs.110).aspx

This class has a function 'GetDistanceTo(Geocoordinate)' which "Returns the distance between the latitude and longitude coordinates that are specified by this GeoCoordinate and another specified GeoCoordinate."

I can use this in my loop, using GeoCordinate objects, to analyse which points are closest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM