Possible Duplicate:
Fastest Way to Find Distance Between Two Lat/Long Points
I have a database that includes the town name and the longitude and latitude coordinate numbers. I have a system where a user enters the pick up location and destination when they want to hire a bus. I am able to query mysql database to bring out the two locations from the hire being made but am not able to get the coordinates of the two locations that are stored in another table on the database. I'm pretty sure the code to work out the distance between the two locations works as it worked with just putting numbers before rather than variables but I cannot figure out what I'm doing wrong. Any help is appreciated, thanks.
$result = mysql_query("SELECT hireid FROM hire ORDER BY customerid DESC LIMIT 1");
while ($row = mysql_fetch_assoc($result)){
$hireid = $row['hireid'];
}
echo "hireid: " . $hireid . "<br/>";
$result = mysql_query("SELECT pickuplocation, destination FROM hire WHERE hireid='$hireid'");
while ($row = mysql_fetch_assoc($result)){
$pickuplocation = $row['pickuplocation'];
$destination = $row['destination'];
}
echo "pickuplocation: " . $pickuplocation . "<br/>";
echo "destination: " . $destination . "<br/>";
$result = mysql_query("SELECT longitude, latitude FROM coordinates WHERE town='$pickuplocation'") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result)){
$long1 = $row['longitude'];
$lat1 = $row['latitude'];
}
"<br/>";
echo "Location1 longitude: " . $long1 . "<br/>";
echo "Location1 latitude: " . $lat1 . "<br/>";
$result = mysql_query("SELECT longitude, latitude FROM coordinates WHERE town='$destination'") or die ('Error: '.mysql_error ());
while ($row = mysql_fetch_array($result)){
$long2 = $row['longitude'];
$lat2 = $row['latitude'];
}
"<br/>";
echo "Location2 longitude: " . $long2 . "<br/>";
echo "Location2 latitude: " . $lat2 . "<br/>";
function getDistanceBetweenPointsNew($lat1, $long1, $lat2, $long2) {
$theta = $long1 - $long2;
$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles') . "<br/>";
}
$point1 = array('lat' => $lat1, 'long' => $long1);
$point2 = array('lat' => $lat2, 'long' => $long2);
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
foreach ($distance as $unit => $value) {
echo $unit.': '.number_format($value,4).'<br />';
}