This is a migrated thread and some comments may be shown as answers.

Is it possible to find nearest location to a given location

1 Answer 92 Views
Map
This is a migrated thread and some comments may be shown as answers.
Rod Yager
Top achievements
Rank 1
Rod Yager asked on 10 Nov 2010, 08:56 PM
Say I have a collection of locations and I want to find out which one is the closest to another specified location, how would I do this?

Thanks,
Rod

1 Answer, 1 is accepted

Sort by
0
Accepted
Andrey
Telerik team
answered on 12 Nov 2010, 04:41 PM
Hi Rod Yager,

You can find out the closest point to the location using calculation of distance between the location and elements of the collection. You can create the LocationRect instance from two locations and then calculate a distance using Width and Height properties.

The sample method is below.
private Location GetNearestLocation(LocationCollection collection, Location point)
{
    double minDistance = double.PositiveInfinity;
    Location nearestLocation = Location.Empty;

    
foreach (Location location in collection)
    {
        LocationRect rect = new LocationRect(location, point);
        rect.MapControl = this.radMap;

        
double distance = Math.Sqrt(rect.Width * rect.Width + rect.Height * rect.Height);

        
if (distance < minDistance)
        {
            minDistance = distance;
            nearestLocation = location;
        }
    }

    
return nearestLocation;
}

Best wishes,
Andrey Murzov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Map
Asked by
Rod Yager
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or