简体   繁体   English

如何在PHP中以公里为单位计算两个位置之间的距离?

[英]How to calculate distance between two locations in kilometers, in php?

I have a table in which I am storing the service and columns of service table is lattitude, longitude ie location of a service. 我有一个表,用于存储服务,服务表的列是纬度,经度,即服务的位置。

Now I want to get the current location of the user and search the services available around 10 kilometers distance to the user. 现在,我想获取用户的当前位置并搜索距用户10公里左右的可用服务。

For this I searched how to calculate the distance between two locations and tried one of the code but it is giving me some syntax error in JSON. 为此,我搜索了如何计算两个位置之间的距离,并尝试了其中一个代码,但这给了我JSON语法错误。

The error is syntax error, (unexpected 'S'), also I don't get the result in kilometers by this formula. 错误是语法错误,(意外的“ S”),我也无法通过此公式获得以千米为单位的结果。

     public function searchVendors($lattitudeFrom,$longitudeFrom)
    {
        $lattitudeTo = -10.4212157;
        $longitudeTo = 28.6031842;


        $dist = $this -> haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000);

        return $dist;

    }

 public function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
    // convert from degrees to radians
    $latFrom = deg2rad($latitudeFrom);
    $lonFrom = deg2rad($longitudeFrom);
    $latTo = deg2rad($latitudeTo);
    $lonTo = deg2rad($longitudeTo);

    $latDelta = $latTo - $latFrom;
    $lonDelta = $lonTo - $lonFrom;

    $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
        cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
    return $angle * $earthRadius;
}

getVendors.php getVendors.php

  <?php

header("Content-type: application/json");

   // if ( $_SERVER['REQUEST_METHOD']=='POST') {
        include_once ("../include/Vendor.php");
    try {

    $con = DB::getConnection();

    $raw = file_get_contents("php://input");
    $data = json_decode($raw, true);

    $longitude = $data->longitude;
    $lattitude = $data->lattitude;

    $v = new Vendor();
    $response = $v -> searchVendors($lattitude,$longitude);


    json_encode($response);


    echo $response;

        if ( $response == null ) {
            $response = json_encode(array("result" => -2, "message" => "Empty result"));
            echo $response;
        } else {
            echo $response;
        }
    } catch(Exception $e) {
        $result = array("result" => -1, "message" => $e -> getMessage());
        echo json_encode($result);
        }
//}
?>

How can I achieve this? 我该如何实现? Can anyone help please.. Very new in php. 任何人都可以帮忙.. PHP中的新手。

php cal_dist(a,b,key = none,spec = km)

Try this code for find distance in KM or MILES 尝试使用此代码查找以KM或MILES为单位的距离

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

      $theta = $lon1 - $lon2;
      $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
      $dist = acos($dist);
      $dist = rad2deg($dist);
      $miles = $dist * 60 * 1.1515;
      $unit = strtoupper($unit);

      if ($unit == "K") {
        return ($miles * 1.609344);
      } else if ($unit == "N") {
          return ($miles * 0.8684);
        } else {
            return $miles;
          }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM