Reputation: 65
click here for image]1i am trying to calculate distance between two places based on latitude and longitude. I stored the latitude and longitude values in my data base. I provide one drop down list for from stand, and another for to stand, i getting the place names from data base. When ever i select the from place and to place i want the distance in another text area. i able to get distance manually, but i need it dynamically. Please help.
This is my code.
<?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>
in above image when i select the from stand, to stand i need distance distance text field. the stand latitude and longitude values are available in database.
Upvotes: 1
Views: 163
Reputation: 5444
Try this...
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>";
Upvotes: 1