简体   繁体   English

如何从坐标列表中计算到给定点的最近坐标

[英]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我的应用程序是用 java 编写的,用于 android 平台

http://developer.android.com/reference/android/location/Location.html 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.检查 distanceTo 或 distanceBetween 方法。

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或者您可以手动计算坐标之间的距离并找到计算距离的最小距离,您可以参考以下链接http://www.zipcodeworld.com/samples/distance.java.ZFC35FDC70D5FC69A369883A82EZC2

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.给定坐标 x,找到它所属的象限,仅计算同一象限中点的距离(如果没有,则从相邻象限添加点,直到成功),选择 k 个最近点 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.检查圆 c(center=x,radius=max(p_i-x)) 是否穿过任何尚未检查的象限,如果是,则计算从这些象限到点的距离。 Return the set of closest k points altogether.完全返回一组最近的 k 点。

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.与其选择圆 c 中的所有象限,不如检查 c 中包含点的最近象限,直到找到 k 个最近点 p_i,以便 c(x,max(p_i-x)) 中的所有象限为空或选中。 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.将最近的象限搜索从 O(n) 加速到 O(log n):您需要实现一个树状结构:4 个象限的象限等,它跟踪每个象限中的点数。 When points move, update it (O(log)).当点移动时,更新它(O(log))。 Anyway, for 200 points this is probably an overkill.无论如何,对于 200 分,这可能是矫枉过正。

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)编辑:而不是“树状结构”,只需使用 hash 表和简单的 hash function,例如:(x div 10^p)

There is a divide and conquer algorithm to find the closes pair of points in O(nlogn).有一种分而治之的算法来找到 O(nlogn) 中的闭合点对。 Although it may be a but overkill for your data set size.尽管对于您的数据集大小而言,这可能有点过分。 More info on it here更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM