简体   繁体   中英

How to calculate the closest co-ordinates to a given point from a list of co-ordinates

Basically i have a users current location. i then have a list of co-ordinates.

How would i go about calculating the nearest set of co-ordinates from the list against the users current location. My application is written in java for hthe android platform

http://developer.android.com/reference/android/location/Location.html

Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);

check for distanceTo or distanceBetween methods.

Or you can manually calculate the distance among the coordinates and find the smallest distance for calculating distance you can refer following link http://www.zipcodeworld.com/samples/distance.java.html

If points on the list are rather uniformly distributed in the area, this should work:

Divide the area into quadrants of a certain size. Keep and update a list of points which reside in each quadrant.

Given a coordinate x, find the quadrant it belongs to, compute distances only for points in the same quadrant (if none there, add points from neighboring quadrants, until success), choose k closest points p_i.

Check if the circle c(center=x,radius=max(p_i-x)) crosses any quadrants that were not checked yet, and if it does, compute distances to points from those quadrants. Return the set of closest k points altogether.

Instead of selecting all quadrants in a circle c, you may want check closest quadrants inside c that contain points, until you find k closest points p_i so that all quadrants in c(x,max(p_i-x)) are empty or checked. Speed up the nearest quadrant search from O(n) to O(log n): you need to implement a tree-like structure: quadrants of 4 quadrants, etc. which tracks the number of points in each quadrant. When points move, update it (O(log)). Anyway, for 200 points this is probably an overkill.

edit: instead of a "tree-like structure" just use hash table and an easy hash function, like: (x div 10^p, y div 10^p)

There is a divide and conquer algorithm to find the closes pair of points in O(nlogn). Although it may be a but overkill for your data set size. More info on it here

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