您的位置:首页 > 编程语言 > PHP开发

php 根据经纬度计算两点之间距离

2013-05-21 18:59 232 查看
网上搜到的代码,贴一下。

<?php
function getdistance( $lat1,$lng1,$lat2,$lng2){
$earthRadius = 6367000;

$lat1 = ($lat1 * pi() )/180;
$lng1 = ($lng1 * pi() )/180;
$lat2 = ($lat2 * pi() )/180;
$lng2 = ($lng2 * pi() )/180;

$calcLongitude = $lng2-$lng1;
$calcLatitude = $lat2 - $lat1;

$stepone = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
$steptwo = 2 * asin(min(1,sqrt($stepone)));

$calculatedDistance = $earthRadius * $steptwo;

return round($calculatedDistance);
}

$la1 = $_GET["la1"];
$ln1 = $_GET["ln1"];

$la2 = $_GET["la2"];
$ln2 = $_GET["ln2"];

echo getdistance($la1,$ln1,$la2,$ln2);
?>


检查过了,正确的。可以放心使用。

下面来讨论一下小数点后精度的问题。

经纬度表示有三种格式,下面是各种格式的精度范围,即 误差。上面这个算法采用第一种,精度最高。

1、 ddd.ddddd

精度为1.1米

2、ddd.mm.mmm

精度为1.85米

3、ddd.mm.ss

精度为30.9米
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: