This project is read-only.

Closest Point to a Point

Feb 26, 2013 at 9:52 AM
I am just starting to use this library and it looks really great.

I need to find the closest Point in a collection to a given Latitude/Longitude location.

The collection is a set of Towns, and I need to find the closest Town to my location.

What is the best way to determine this?
Feb 26, 2013 at 12:20 PM
Edited Feb 26, 2013 at 12:21 PM
IFeature GetClosestPoint(IFeature mypoint)
{
    IMapPointLayer layer = map1.Layers[0] as IMapLineLayer;
    double distance = 0.0;
    IFeature closest = null;
    foreach(IFeature feature in layer.DataSet.Features)
    {
        double d = feature.Distance(mypoint);
        if(distance == 0.0)
        {                   
            distance = d;
            closest = feature;
        }
        else if(d < distance)
        {                   
            distance = d;
            closest = feature;
        }
    }
return closest;
}
Feb 27, 2013 at 9:54 AM
Thank You..

That is basically how I solved it. Below cvDataset is the data, and r.Get() returns the value for the given column in the dataset
    List<Coordinate> list = (from DataRow r in cvDataSet let lat = (double) r.Get(maLat) let lon = (double) r.Get(maLon) select new Coordinate(lat, lon)).ToList();

    Point pt = new Point(latitude, longitude);
    Coordinate closestPt = (from Coordinate p in list let d = pt.Distance(p) orderby d select p).First();