简体   繁体   中英

How do I calculate distance/proximity between one location and another (c#)

Hi I'm wondering how to calculate very accurately when a user gets into proximity of say 30 feet of another specified location, there may be many specified locations so what would the best way to do be?

*example scenario; mobile app user moves into proximity (30 ft) of one of its apps many specified locations..(how would I calculate that proximity between 2 locations ie the mobile user and other locations)

thanks in advance

the best approach is to use a "spatially enabled" data structure to store the list of locations to test. the distance in itself is computed using well-known formulas documented everywhere on the net ( great circle distance , geographical distance , vincenty's formulae ...)

the easiest such structure is the kd-tree : it allows to store a set of coordinates and to quickly find the nearest point to a given location. the structure is easy to grasp, the operations are quite easy to program (you may even find a library which does all this), and it is fast enough for mobile needs.

other than a kd-tree, your problem resolve to a nearest-neighbor search, which is well documented on wikipedia . you will find there plenty of data structures to store your locations, and to search for the nearest location.

in any case, do NOT perform a full-search (comparing your current location to the whole set of stored locations). the time complexity of such an algorithm depends heavily on the number of location you test, the less comparison you make, the faster you find a solution. a full-search involves too many comparison, and if the number of location is too high compared to the procesing power of your device, you will make a mobile device crawl down to a halt before finding the nearest location.

Going back to my high school math knowledge, you can use Pythagorean theorem to get that distance. Take point a and b, square them and add together. Then take the square root.

var distance = Math.Sqrt(a*a + b*b);

To expand on Kon's answer above, this is exactly what I started doing for an app I'm working on at the moment. It is probably the best way to work with a relatively low number of waypoints.

In my app, however, I could have tens of thousands of waypoints to compare so rather than do the maths over and over I expanded on the idea slightly. Since all the points are within the UK, I picked three fixed points (in my case, a point off Northern Scotland, one off Cornwall, and one in the English Channel near Dover) and when I save a new waypoint I calculate the distances to each of these fixed points. If I need to pull out points close to another known location, I can compare the distances from these waypoints fairly simply within the database and only pull back the ones that are close. By using triangulation the maths involved is trivial and I saw significant speed increases.

EDIT I'm also going to check out Adrien Plisson's NNS link now to see if that will help me even further. :)

The easiest way to do this is to use the GeoCoordinate class which has a method GetDistanceTo which takes another method and returns the dinstance between the two.

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