从地理坐标计算本地用户的最有效方法是什么?

发布于 2024-11-05 07:15:23 字数 252 浏览 0 评论 0原文

[编辑] - 正如评论中向我指出的那样,我应该注意到我将使用基于 LAMP 的架构,即 MySQL 数据库。抱歉忘记提及这一点。

我必须为 iPhone 应用程序创建一个 PHP 后端。该应用程序发送用户坐标并请求关闭 10 个本地用户。

我只是想知道确定这一点的最有效方法是什么,我不想扫描整个用户表,计算他们的地理坐标与目标和目标之间的距离。从最低到最远对它们进行排序。

谁能提出一个更优雅的解决方案,而不是扫描所有用户?感谢您抽出时间。

[edit] - as pointed out to me in the comments I should have noted that I will be using a LAMP based architecture, meaning a MySQL database. Sorry about forgetting to mention this.

I have to create a PHP backend for an iPhone app. The app sends up the users coords and requests the closes 10 local users.

I'm just wondering what is the most efficient way to determine this, I dont want to have to scan the entire table of users, calculate the distance between their geo-coords and the target & order them from lowest to farthest.

Can anyone suggest a more elegant solution instead of scanning all the users? Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔戏命师 2024-11-12 07:15:23

您可以按近似的纬度和经度(例如,按整度或十分之一度,或适合您所获得的分布的任何比例)对用户进行存储,然后从查询用户的存储桶向外工作。

不过,不确定额外的复杂性是否值得,除非强力方法确实非常消耗性能。

You could bucket the users by approximate latitude and longitude (e.g. by whole degrees, or tenths of degrees, or whatever scale is appropriate to the distribution you've got) and then work outward from the querying user's bucket.

Not sure the extra complexity is worth it, though, unless the brute force approach is really a performance hog.

悲凉≈ 2024-11-12 07:15:23

最有效的方法是使用空间索引或空间填充曲线。空间索引将 2d 复杂性降低到 1d 复杂性,因此有助于细分表面。您想查找 Nick 的空间索引四叉树希尔伯特曲线博客。

The most efficient way is to use a spatial index or a space-filling-curve. A spatial index reduce the 2d complexity to a 1d complexity thus it help subdivide the surface. You want to look for Nick's spatial index quadtree hilbert curve blog.

暖风昔人 2024-11-12 07:15:23

有多种方法可以做到这一点。这是在 sql 查询中执行此操作的一种方法。

$sql = "Select id, abs($currlat - latitude) + abs($currlon - longitude) as distance from geo_loc_table order by distance asc";

这样做的问题是它效率不太高。我建议您使用某种形式的索引软件来为您完成这项工作。狮身人面像可能是一个不错的选择。

There are multiple ways of doing this. Here is one way to do it in a sql query.

$sql = "Select id, abs($currlat - latitude) + abs($currlon - longitude) as distance from geo_loc_table order by distance asc";

The issue with this is that it is not overly efficient. I would recommend getting some form of indexing software that can do the work for you. Sphinx might be a good fit.

心的位置 2024-11-12 07:15:23

由于地球曲线,您必须进行一些计算

SELECT id, 6371 * ACos(Cos(RADIANS(users.Lat)) * Cos(RADIANS(CurrentLat)) * Cos(RADIANS(CurrentLng) - RADIANS(users.Lng)) + Sin(RADIANS(users.Lat)) * Sin(RADIANS(CurrentLat)) ) AS distance
FROM users
ORDER BY distance
LIMIT 10;

距离单位为Km,如果您想要英里,请将6371替换为3959

Due to earth curve, you must do some calculation

SELECT id, 6371 * ACos(Cos(RADIANS(users.Lat)) * Cos(RADIANS(CurrentLat)) * Cos(RADIANS(CurrentLng) - RADIANS(users.Lng)) + Sin(RADIANS(users.Lat)) * Sin(RADIANS(CurrentLat)) ) AS distance
FROM users
ORDER BY distance
LIMIT 10;

distance is in Km, if you want Miles, replace 6371 by 3959

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文