从地理坐标计算本地用户的最有效方法是什么?
[编辑] - 正如评论中向我指出的那样,我应该注意到我将使用基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以按近似的纬度和经度(例如,按整度或十分之一度,或适合您所获得的分布的任何比例)对用户进行存储,然后从查询用户的存储桶向外工作。
不过,不确定额外的复杂性是否值得,除非强力方法确实非常消耗性能。
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.
最有效的方法是使用空间索引或空间填充曲线。空间索引将 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.
有多种方法可以做到这一点。这是在 sql 查询中执行此操作的一种方法。
这样做的问题是它效率不太高。我建议您使用某种形式的索引软件来为您完成这项工作。狮身人面像可能是一个不错的选择。
There are multiple ways of doing this. Here is one way to do it in a sql query.
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.
由于地球曲线,您必须进行一些计算
距离
单位为Km,如果您想要英里,请将6371替换为3959Due to earth curve, you must do some calculation
distance
is in Km, if you want Miles, replace 6371 by 3959