2016-08-15 119 views
2

我想顯示酒店和用戶之間的距離。 計算酒店和用戶地址之間的距離我已經使用了以下代碼。距離值我點擊了按鈕。如何使用php計算兩地之間的距離?

1)首先我得到使用IP地址並從IP地址獲取所有用戶地址信息。

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

這個電話給我詳細的城市,國家,countryCode,緯度,經度,郵編等。保存經緯度。 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和總時間節省$時間。它還提供了您在$response_a中查看的其他詳細信息。打印$response_a的值。

但所有的時間它給我不正確的距離。

+2

一旦你有兩個緯度/經度,你只需使用*大圓距離公式*;這很簡單,查看它。無需將座標轉換爲地址並使用API​​。說完這句話,是的,在IP *上查找一個位置*只會給你非常非常好的***粗糙的位置,準確性只能達到城市或州級別;在幾公里內絕對不準確。 – deceze

回答

0

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; 
} 
?> 

所有的積分都會發送給在上面鏈接的線程中發佈實現的人。

+0

謝謝您的回答 我想通過公路計算距離。我已經使用你給的功能。但它也給我錯誤的距離。 –

0

如果你想要兩點之間的距離,你可以使用that,但如果你想要一個行程的距離,你可能想要使用Google Directions API或一些等價的。

+0

感謝您的回覆 我已經使用google map –

+0

的距離矩陣API那麼它有什麼問題呢? – vincenth

+0

@vincenth它不準確,它會返回距離真實位置超過50公里的粗糙結果。爲了您的信息,我正在處理與Vimal Patel相同的項目 – papacico

相關問題