What I want to do - I have a few schools in a database (three to be exact, just to get me started) each with their own latitude and longitude. When a User enters in their postcode which in turn produces their latitude and longitude, I want the page to come back telling them their nearest school and their distance away from it.
I followed a following tutorial which basically tells a user what their longitude and latitude is.
Original source: http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/
So I have the following:
-
Gmap.js - A JavaScript file (that is pulled in by the HTML page).
Original source: http://www.tomanthony.co.uk/demo/geocode_uk_postcode/gmap.js -
Main.html - An HTML page that pulls in Gmap.gs in order to calculate a Users longitude and latitude depending on the postcode that they have entered into a text box and puts the longitude and latitude into their respective textfields.
In order to calculate the distance between two points, I edited some PHP code to calculate the distance between two points given the latitude/longitude of those points. I’m at a very early stage with this code – it doesn’t do a great deal at the moment other than produce a distance in miles.
Original source: http://www.zipcodeworld.com/samples/distance.php.txt
Distance.php
[php]<?php
/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/
/:: :/
/:: this routine calculates the distance between two points (given the :/
/:: latitude/longitude of those points). it is being used to calculate :/
/:: the distance between two zip codes or postal codes using our :/
/:: zipcodeworld™ and postalcodeworld™ products. :/
/:: :/
/:: definitions: :/
/:: south latitudes are negative, east longitudes are positive :/
/:: :/
/:: passed to function: :/
/:: lat1, lon1 = latitude and longitude of point 1 (in decimal degrees) :/
/:: lat2, lon2 = latitude and longitude of point 2 (in decimal degrees) :/
/:: unit = the unit you desire for results :/
/:: where: ‘m’ is statute miles :/
/:: ‘k’ is kilometers (default) :/
/:: ‘n’ is nautical miles :/
/:: united states zip code/ canadian postal code databases with latitude & :/
/:: longitude are available at http://www.zipcodeworld.com :/
/:: :/
/:: For enquiries, please contact [email protected] :/
/:: :/
/:: official web site: http://www.zipcodeworld.com :/
/:: :/
/:: hexa software development center © all rights reserved 2004 :/
/:: :/
/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/
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;
return $miles;
}
echo distance(32.9697, -96.80322, 29.46786, -98.53506, “m”) . " miles
";
?>
[/php]
Can you advise me as to what to do next with this PHP file in order to do as per the first paragraph of my post? I’m not sure how to break down what I need to do, where to do what, and what to do first.
Thanks, and if there are any questions just let me know.