3.5 KiB
geocode_class
NOTE: There is a file missing (class/key.php)
You will need to get your own Google API Key
Change the YOUR KEY HERE with your key.
Keep the key= part as it is required for the string.
key.php
<?php
CONST API_KEY = 'key=YOUR KEY HERE';
*** Place this file in the same directory as the class file (geocode.php)
Currently this class has three functions. Two require a Google API Key to work.
1) straight_line_distance($lat1, $lon1, $lat2, $lon2, $unit='')
This will calculate the distance between two sets of Latitudes and Longitudes. using a straight line or as the crow flies.
Using $unit you can have it display the distance in (Blank=Miles, K=Kilometers, N=Nautical Miles). Default=Blank or Miles.
Output: Distance Value
Google API Required Functions.
2) getLatLong($address)
This will return a Latitude and Longitude for a given address in a array format.
Output:
$Arr['status'] - Returns Google status for call. Normal = 'OK'
$Arr['lat'] - Returns the Latitude.
$Arr['lon'] - Returns the Longitude.
3) getDistance($origin, $destination, $mode='driving', $unit='imperial')
This returns the distance and duration it will take from the Origin to the Destination addresses.
Using $mode you can select from three types (driving, walking, bicycling). Default = driving.
Using $unit you can select from (imperial = miles or metric = kilometers). Default = imperial.
Output:
$Arr['status'] - Returns Google status for call. Normal = 'OK'
$Arr['distance'] - Returns the distance between origin and the destination.
$Arr['duration'] - Returns how long it will take to get from the origin to the destination.
geocode-example.php
<?php
// == GEOCODE CLASS ====================================================================================================
require_once 'class/geocode.php';
// -- Init Class
$geocode = new geocode();
// -- Straight Line Distance -------------------------------------------------------------------------------------------
echo 'Straight Line Distance between two Lat/Lon <br/><br/>';
echo $geocode->straight_line_distance(39.10968, -94.57949, 39.17521, -94.53957) ;
echo '<br/><br/>' . str_repeat('=',60) . '<br/>';
// -- Get the Lat/Lon of a address -------------------------------------------------------------------------------------
$address = '1600 Pennsylvania Ave NW, Washington, DC 20500 <br/><br/>';
echo 'Lat/Lon of address: ' . $address;
$latlong = $geocode->getLatLong($address);
echo 'STATUS: ' . $latlong['status'] . '<br />';
echo 'LAT: ' . $latlong['lat'] . '<br />';
echo 'LON: ' . $latlong['lon'] . '<br />';
echo '<br/>' . str_repeat('=',60) . '<br/>';
// -- Drive Distance ---------------------------------------------------------------------------------------------------
$origin = '1600 Pennsylvania Ave NW, Washington, DC 20500 <br/>';
$destination = '1313 Mockingbird Lane, North Hollywood, CA 91602 <br/>';
echo 'Drive Distance: <br/>';
echo 'From: ' . $origin . ' to: ' . $destination . '<br/>';
$dist = $geocode->getDistance($origin, $destination, 'driving', 'imperial');
echo 'STATUS: ' . $dist['status'] . '<br/>';
echo 'DISTANCE: ' . $dist['distance'] . '<br/>';
echo 'DURATION: ' . $dist['duration'] . '<br/>';
echo '<br/>' . str_repeat('=',60) . '<br/>';
// == END SCRIPT =======================================================================================================