如何在两个地方之间获得准确或(接近准确)的距离? MySQL的/ PHP

时间:2009-06-12 11:43:20

标签: mysql geocoding

SELECT postcode,lat,lng,     截短(         (度(ACOS             (SIN(弧度(lat))                 *                 罪(弧度('。$ latitude。'))                 +                 COS(弧度(lat))                 *                 cos(弧度('。$ latitude。'))                 *                 cos(弧度(lng - ('。$ longitude。')))                 )             )             * 69.172),2)             作为距离             来自myData
此查询计算距离(以英里为单位)。但是当我在google地图上检查相同纬度和经度的距离时,我的结果不匹配。如果距离大约10英里,那么我的结果有点准确,但结果是错误的(例如,我的结果显示13英里,谷歌显示22英里的相同邮政编码值)

我从http://forums.mysql.com/read.php?23,3868,3868#msg-3868

收到了此查询

我怎样才能准确无误。有任何想法吗? 谢谢你的帮助。

更新

我在PHP中尝试了@Blixt代码。拿起2个样本邮政编码和他们的拉特长期

//B28 9ET
$lat1 = 52.418819;
$long1 = -1.8481053;

//CV5 8BX
$lat2 = 52.4125573;
$long2 = -1.5407743;

$dtr = M_PI / 180;
$latA = $lat1 * $dtr;
$lonA = $long1 * $dtr;
$latB = $lat2 * $dtr;
$lonB = $long2 * $dtr;
$EarthRadius = 3958.76; //miles
echo $distance = $EarthRadius * acos(cos($latA) * cos($latB) * cos($lonB - $lonA) + sin($latA) * sin($latB));

结果:

我的应用程序 - 12.95英里

Google - 17.8英里

任何想法如何做到正确?

1 个答案:

答案 0 :(得分:1)

看看this source code。当我针对各种其他测量服务进行测试时,它似乎得到了相同的结果。这是C#,但数学应该很容易转换。

以下是相关部分:

public const double EarthRadius = 6371.0072; // Kilometers
//public const double EarthRadius = 3958.76; // Miles

/* ... */

const double dtr = Math.PI / 180;
double latA = this.Latitude * dtr;
double lonA = this.Longitude * dtr;
double latB = other.Latitude * dtr;
double lonB = other.Longitude * dtr;

return GpsLocation.EarthRadius * Math.Acos(Math.Cos(latA) * Math.Cos(latB) * Math.Cos(lonB - lonA) + Math.Sin(latA) * Math.Sin(latB));

注意:地球不是完美的球形,因此使用的常数可能不同。没有简单的方法可以使测量真正准确。请参阅Wikipedia