2013-06-24 120 views
0

我已經使用了下面的代碼,但是這給了我從源到目的地的空中距離,但是我需要道路距離,請幫助我這個人。計算從源經緯度到目的地的道路距離

這項工作,但這個空中距離,但我需要街/路的距離。

$center_lat =$_POST['lat']; 

//19.159519;// 
$center_lng =$_POST['lng'];//72.995782;// 
$radius = '3963.0'; 
$range=$_POST['range']; 
//echo $center_lng; 
// Set the active mySQL database 
$db_selected = mysql_select_db("mapdb", $connection); 

// Search the rows in the markers table 
$query = sprintf("SELECT name,lat, lng, (3959 * acos(cos(radians('%s')) * cos(radians(lat)) * cos(radians(lng) - radians('%s')) + sin(radians('%s')) * sin(radians(lat)))) AS distance FROM hospitals HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20", 
    mysql_real_escape_string($center_lat), 
    mysql_real_escape_string($center_lng), 
    mysql_real_escape_string($center_lat), 
    mysql_real_escape_string($radius)); 
$result = mysql_query($query); 

if (!$result) { 
    die("Invalid query: " . mysql_error()); 
} 
else 
{ 
    echo "Done"; 
} 
// header('Content-type: application/json'); 
//$output = array(); 
$i=0; 

while($row = mysql_fetch_assoc($result)) { 
    if($range > 0) 
     { 
     $theta = $center_lng - $row['lng']; 
     $distance = (sin(deg2rad($center_lat)) * sin(deg2rad($row['lat']))) + (cos(deg2rad($center_lat)) * cos(deg2rad($row['lat'])) * cos(deg2rad($theta))); 
     $distance = acos($distance); 
     $distance = rad2deg($distance); 
     $distance = $distance * 60 * 1.1515; 
     $distance = $distance *1.609344; 
     if($range > round($distance,2)) 
     { 
      $info[$i] = "\"".$row['lat']."&".$row['lng']."@".$row['name']; 
      $i=$i+1;    
     } 
     $myTwitterResult1 = array($info); 
    } 
    else 
    { 
     $info[$i] = "\"".$row['lat']."&".$row['lng']."@".$row['name']; 
     $i=$i+1; 
     $myTwitterResult1 = array($info); 
    } 
} 
    $myJSONTweets = json_encode($myTwitterResult1); 
    echo $myJSONTweets; 
+0

道路距離沒有神奇的公式。首先你需要一張道路地圖 – DevZer0

+0

沒有這樣做,我找不到距離? – hemant

+0

你可以利用外部API,如谷歌地圖 – DevZer0

回答

0

此功能可以幫助你

function distance($lat1, $lon1, $lat2, $lon2, $unit) 
{ 
    $angle = $lon1 - $lon2; 
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) *  cos(deg2rad($lat2)) * cos(deg2rad($angle)); 
$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; 
    } 
} 
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>"; 

更多信息here

+0

-Rajeev Ranjan我在我的代碼中使用了相同的功能,你可以看到它,我不想要這個。這讓我回到了空中距離。 – hemant

+0

@hemant在這種情況下,找到一種方法來使用谷歌地圖Api –

相關問題