2015-11-12 421 views
1

点击此处查看图像] 1我想根据经度和纬度计算两地之间的距离。我将纬度和经度值存储在我的数据库中。我提供了一个从站点下拉列表,另一个站点,我从数据库中获取地名。当我选择从地方和地方我想在另一个文本区域的距离。我能够手动获得距离,但我需要动态。请帮忙。距离计算

这是我的代码。

<?php 
function GetDrivingDistance($lat1, $long1, $lat2, $long2) 
{ 
$distance=""; 
$duration=""; 
$url = "https://maps.googleapis.com/maps/api/distancematrix 
/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."& 
mode=driving&language=pl-PL"; 
$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); 
// check the results 
if($response_a['status'] != 'OK' || $response_a['rows'][0]['elements'] 
[0]['status'] == "NOT_FOUND") 
{ 
return false; 
} 
$dist = $response_a['rows'][0]['elements'][0]['distance']['text']; 
$time = $response_a['rows'][0]['elements'][0]['duration']['text'] 
return array('distance' => $dist, 'time' => $time); 
} 
$dist = GetDrivingDistance(17.5002541,78.4769027,17.4484114,78.3631118); 
if($dist) 
{ 
echo 'Distance: '.$dist['distance'].' 
Travel time duration: '.$dist['time'].''; 
} 
else 
{ 
echo "error"; 
} 
// php select option value from database 
$hostname = "localhost"; 
$username = "root"; 
$password = ""; 
$databaseName = "pbits"; 
// connect to mysql database 
$connect = mysqli_connect($hostname, $username, $password, 
$databaseName); 
// mysql select query 
$query = "SELECT standId, standName FROM `stands`"; 
// for method 1 
$result1 = mysqli_query($connect, $query); 
$result2 = mysqli_query($connect, $query); 
?> 
<html> 
<head> 
<title>dynamic dropdown </title> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
</head> 
<body> 
<table> 
<tr> 
<td> 
From Stand: 
</td> 
<td> 
to stand: 
</td> 
<td> 
Distance: 
</td> 
</tr> 
<tr> 
<td> 
<select> 
<?php while($row1 = mysqli_fetch_array($result1)):;?> 
<option value="<?php echo $row1[0];?>"><?php echo $row1[1];?></option> 
<?php endwhile;?> 
</select> 
</td> 
<td> 
<select> 
<?php while($row1 = mysqli_fetch_array($result2)):;?> 
<option value="<?php echo $row2[0];?>"><?php echo $row2[1];?></option> 
<?php endwhile;?> 
</select> 
</td> 
<td> 
<input type="text" name="distance" value=""> 
</td> 
</tr> 
</table> 
</body> 
</html> 

在上面的图片,当我选择从立场,站立我需要距离文本字段。数据库中提供了经纬度值。

回答

1

尝试......

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

echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>"; 
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>"; 
echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>"; 

演示:https://eval.in/467537

+0

感谢您的建议..我手动越来越距离,但我需要dyanamically,基于从和站立的地方上。 – Meda

+0

动态的意思是? –

+0

我从中选择,以在下拉列表中选择位置,我选择我需要这两个位置之间的距离,这些位置的纬度和经度值存储在我的数据库中 – Meda