PHP MySQL从GPS获取半径用户位置中的位置 [英] PHP MySQL get locations in radius user's location from GPS

查看:154
本文介绍了PHP MySQL从GPS获取半径用户位置中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我的数据库中有汽车事故.这些事件具有纬度和经度.在使用GPS的手机上,我可以得到用户的位置及其坐标.用户可以选择他想知道周围是否有事件的半径.因此,假设他想知道他周围2英里处的事件.

I have in my database car incidents for example. These incidents have a latitude and longitude. On a mobile using the GPS, I get the user's location with his coordinates. The user can select a radius that he wants to know if there are incidents around him. So let's say he want to know incidents 2 miles around him.

因此,我从电话向网络服务发送了用户的纬度,经度和他选择的半径.我需要进行SQL查询,以获取围绕用户2英里的事故.

So I send from the phone to a web service the user's latitude, longitude and the radius he selected. I need to make a SQL query to get the incidents 2 miles around the user.

您是否知道该怎么做?

推荐答案

计算距离在计算上非常昂贵.返回大量数据集也不是一个好主意-特别是考虑到PHP的性能不是很好.

Calculating the distance is pretty computationally expensive, as others have said. Returning huge datasets is also not a very good idea - specially considering PHP isn't that great in performance.

我会使用启发式方法,例如通过简单的加法和减法来近似距离.

I would use a heuristic, like approximating the distance with simple addition and subtraction.

1分钟= 1.86公里= 1.15 英里

1 minute = 1.86 kilometers = 1.15 miles

只需在事件范围内搜索数据库(实际上是正方形而不是圆形),然后就可以使用PHP进行处理.

Just search the db with incidents within that range (effectively a square, rather than a circle), and then you can work on those with PHP.

EDIT :这是另一种选择;一种近似的方法,它的计算成本更低:

EDIT: Here's an alternative; an approximation that's way less computationally expensive:

以英里为单位的大概距离:

Approximate distance in miles:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1) 
and y = 53.0 * (lon2 - lon1) 

您可以通过添加余弦数学函数来提高此近似距离计算的准确性:

You can improve the accuracy of this approximate distance calculation by adding the cosine math function:

改进的近似距离(以英里为单位):

Improved approximate distance in miles:

sqrt(x * x + y * y)

where x = 69.1 * (lat2 - lat1) 
and y = 69.1 * (lon2 - lon1) * cos(lat1/57.3) 

来源: http://www.meridianworlddata.com/Distance-Calculation.asp

EDIT 2 :我用随机生成的数据集进行了一堆测试.

EDIT 2: I ran a bunch of tests with randomly generated datasets.

  • 这3种算法的准确性差异很小,尤其是在短距离时
  • 最慢的算法(具有全部trig函数的算法)比其他两个算法慢4倍.

绝对不值得.只是一个近似值.

Definitely not worth it. Just go with an approximation.

代码在这里: http://pastebin.org/424186

这篇关于PHP MySQL从GPS获取半径用户位置中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆