【问题标题】:How to calculate distance between two places using php?如何使用php计算两个地方之间的距离?
【发布时间】:2016-12-21 09:27:53
【问题描述】:

我想显示酒店和用户之间的距离。 为了计算酒店和用户地址之间的距离,我使用了以下代码。我在按钮点击时获得的距离值。

1) 首先我得到了使用 IP 地址并从 IP 地址获取所有用户地址信息。

$ipAddress  = $_SERVER['REMOTE_ADDR'];
$result = json_decode(file_get_contents("http://ip-api.com/json/{$ipAddress}"));

这个电话会告诉我城市、国家、国家代码、纬度、经度、邮编等详细信息。保存纬度和经度。

$user_latitude = $result->lat;
$user_longitude = $result->lon;

2) 我的数据库中已经有酒店的纬度和经度。

now I have four value. User latitude/longitude and Hotel latitude/longitude
$lat1   =   $user_latitude;
$long1  =   $user_longitude;
$lat2   =   $hotel_latitude;
$long2  =   $hotel_longitude;

3) 之后使用谷歌地图 API 我得到了(用户和酒店)的完整地址

$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng={$lat1},{$long1}&sensor=true";
$url2 = "http://maps.googleapis.com/maps/api/geocode/json?latlng={$lat2},{$long2}&sensor=true";

# Get address of User
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$address1 = $response_a['results'][0]['formatted_address'];

# Get address of Hotel
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$address2 = $response_a['results'][0]['formatted_address'];

$find = array("'",'"');
$replace = array("","");
$address1 = str_replace($find,$replace,$address1);
$address2 = str_replace($find,$replace,$address2);

4) 最后我使用了谷歌的距离矩阵 API 并获得了两点之间的距离。

$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&alternatives=true&mode=driving&language=de-DE&sensor=false";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);

$distance = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];

5) 距离保存在$distance 中,总时间保存在 $time 中。它还提供了您在$response_a 中查看的其他详细信息。打印$response_a 的值。

但它总是给我不正确的距离。

【问题讨论】:

  • 一旦有了两个纬度/经度,您只需使用大圆距离公式;很简单,查一下。无需将坐标转换为地址并使用 API。话虽如此,是的,通过 IP 查找位置只会为您提供非常非常 veeeery 粗略的位置,并且准确性只会很好直至城市或州一级;肯定不会精确到几公里以内。

标签: php distance


【解决方案1】:

This stackoverflow thread 有多个Haversine formula 的实现,用于计算距离。在该线程中,还包含一个 PHP 实现,我在下面公然复制并粘贴了它。

<?php function distance($lat1, $lon1, $lat2, $lon2) {

    $pi80 = M_PI / 180;
    $lat1 *= $pi80;
    $lon1 *= $pi80;
    $lat2 *= $pi80;
    $lon2 *= $pi80;

    $r = 6372.797; // mean radius of Earth in km
    $dlat = $lat2 - $lat1;
    $dlon = $lon2 - $lon1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $km = $r * $c;

    //echo '<br/>'.$km;
    return $km;
}
?>  

所有功劳归于在上面链接的线程中发布实现的人。

【讨论】:

  • 感谢您的回答我想通过公路计算距离。我已经使用了您提供的功能。但它也给了我错误的距离。
【解决方案2】:

如果你想要两点之间的距离,你可以使用that,但是如果你想要一个行程的距离,你可能想要使用Google Directions API或类似的东西。

【讨论】:

  • 谢谢你的回答我已经使用谷歌地图的距离矩阵API
  • @vincenth 它不准确,它返回的粗略结果与实际位置相差超过 50 公里。供您参考,我正在与 Vimal Patel 开展相同的项目
  • 那你可以试试其他服务,我认为 OpenStreetMap 提供了等价的服务,但我无法进一步帮助你。祝你好运。
猜你喜欢
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2019-02-16
  • 2016-07-01
  • 1970-01-01
  • 2011-12-24
相关资源
最近更新 更多