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 applicati开发者_如何转开发on 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
精彩评论