2017-05-09 111 views
-4

假設我有兩組座標,我從地圖源通過android應用程序獲得這些座標並將其發送到服務器,現在服務器端將計算它們之間的距離和打印。如何找到兩組座標之間的距離在PHP

+0

你可以分享一些代碼 –

+0

沒有遺憾的我沒有我現在想要發起的任何代碼 –

回答

0

嘗試這個

<?php 

$points = array 
(
    "point1" => "0,55" , 
    "point2" => "0,52" 
); 

function get_distance($inputs) 
{ 
    $count = 1; 
    foreach($inputs as $x => $y) 
    { 
     echo $x." = (".$y.")<br>"; 
     $p[$count] = explode(",",$y); 
     $count++; 
    } 

    $x1 = $p[1][0]; 
    $y1 = $p[1][1]; 

    $x2 = $p[2][0]; 
    $y2 = $p[2][1]; 

    $distance = sqrt(pow(($x2 - $x1) , 2) + pow(($y2 - $y1) , 2)); 
    echo $distance; 
} 

get_distance($points); 

?> 
+0

我已經用低點i-e「point1」=>「0,5」,「point2」=>「0,15」嘗試過它。它工作得很好,謝謝 –

+0

它給了我輸出10這是我的必要答案再次感謝 –

+0

如果包含在函數get_points中的距離計算,而不是單獨分割座標和計算距離會不會更容易? –

0

試試這個:

function calculateDistance($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; 
    } 
} 

,你可以這樣調用它:

calculateDistance($latitude1, $longitude1, $latitude2, $longitude2, "K"); 

Reference