2 changed files with 139 additions and 0 deletions
@ -0,0 +1,112 @@ |
|||
<?php |
|||
/**************************************************** |
|||
* Geocode Class - Collection of geocoding Utilities |
|||
* |
|||
* Created By: Rick Hays |
|||
* Date: 2011-09-12 |
|||
* |
|||
* Revisions: |
|||
* 2011-09-12 RLH Added "straight_line_distance" |
|||
* 2011-09-12 RLH Added "getLatLong" |
|||
* |
|||
*****************************************************/ |
|||
require_once 'key.php'; |
|||
|
|||
class geocode |
|||
{ |
|||
/******************************************************************************************************************* |
|||
* straight_line_distance - Returns Straight Line distance between two sets of (LAT/LONGS) |
|||
* |
|||
* @param real $lat1 - Latitude of Location #1 |
|||
* @param real $lon1 - Longitude of Location #1 |
|||
* @param real $lat2 - Latitude of Location #2 |
|||
* @param real $lon2 - Longitude of Location #2 |
|||
* @param string $unit - Unit of measure [DEFAULT = statute miles] ('K'=kilometers, 'N'=nautical miles) |
|||
* |
|||
* @return float - returns the distance in the desired unit of measure. |
|||
*/ |
|||
public function straight_line_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); } // Return in Kilometers |
|||
else if ($unit == "N") { return ($miles * 0.8684); } // Return in Nautical Miles |
|||
else { return $miles; } // Return in Miles |
|||
} |
|||
|
|||
/******************************************************************************************************************* |
|||
* getLatLong - Converts a string with a street address into Lat/Long coordinates using a Google call. |
|||
* |
|||
* @param string $address - Text address to convert. |
|||
* |
|||
* @return array |
|||
*/ |
|||
public function getLatLong($address) |
|||
{ |
|||
if (!is_string($address))die("All Addresses must be passed as a string"); |
|||
$_url = sprintf('https://maps.googleapis.com/maps/api/geocode/json?address=%s&'.API_KEY,rawurlencode($address)); |
|||
$_result = false; |
|||
if($_result = file_get_contents($_url)) |
|||
{ |
|||
$decode = json_decode($_result,true); |
|||
$_coords['status'] = $decode['status']; |
|||
$_coords['lat'] = $decode['results'][0]['geometry']['location']['lat']; |
|||
$_coords['lon'] = $decode['results'][0]['geometry']['location']['lng']; |
|||
return $_coords; |
|||
}else die('Nothing was returned'); |
|||
|
|||
} |
|||
|
|||
/******************************************************************************************************************* |
|||
* @param $origin |
|||
* @param $destination |
|||
* @param string $mode (driving, walking, bicycling) |
|||
* @param string $units (metric, imperial) |
|||
* |
|||
* @return array |
|||
* @example - https://maps.googleapis.com/maps/api/distancematrix/json?'.$this->api_key.'&origins='.rawurlencode($origin).'&destinations='.rawurlencode($destination).'&mode=driving&units=imperial&sensor=false |
|||
* |
|||
*/ |
|||
public function Get_Distance($origin, $destination, $mode='driving', $unit='imperial') |
|||
{ |
|||
// Set defaults to test for |
|||
$modes = array('driving', 'walking', 'bicycling'); |
|||
$units = array('metric', 'imperial'); |
|||
|
|||
// Test that addresses are present |
|||
if (!is_string($origin) || !is_string($destination))die("All Addresses must be passed as a string"); |
|||
|
|||
// Test that Mode is present and of correct value |
|||
$mode = strtolower($mode); |
|||
if (!in_array($mode, $modes))die("Mode not correct"); |
|||
|
|||
// Test that Unit is present and of correct value |
|||
$unit = strtolower($unit); |
|||
if (!in_array($unit, $units))die("Unit not correct"); |
|||
|
|||
// Prep string for send ie(remove spaces and other objects) |
|||
$_origin = rawurlencode($origin); |
|||
$_destination = rawurlencode($destination); |
|||
$_mode = rawurlencode($mode); |
|||
$_unit = rawurlencode($unit); |
|||
|
|||
$_url = sprintf('https://maps.googleapis.com/maps/api/distancematrix/json?%s&origins=%s&destinations=%s&mode=%s&units=%s&sensor=false',API_KEY, rawurlencode($origin), rawurlencode($destination), $_mode, $_unit); |
|||
if($_result = file_get_contents($_url)) |
|||
{ |
|||
$decode = json_decode($_result,true); |
|||
$_dist['status'] = $decode['status']; |
|||
$_dist['distance'] = $decode['rows'][0]['elements'][0]['distance']['text']; |
|||
$_dist['duration'] = $decode['rows'][0]['elements'][0]['duration']['text']; |
|||
return $_dist; |
|||
}else die('Nothing was returned'); |
|||
} |
|||
|
|||
} // End Class GEOCODE |
|||
|
|||
|
|||
|
|||
|
@ -0,0 +1,27 @@ |
|||
<?php |
|||
// == GEOCODE CLASS ==================================================================================================== |
|||
require_once 'class/geocode.php'; |
|||
// -- Init Class |
|||
$geocode = new geocode(); |
|||
|
|||
// -- Straight Line Distance ------------------------------------------------------------------------------------------- |
|||
echo $geocode->straight_line_distance(39.10968, -94.57949, 39.17521, -94.53957) ; |
|||
echo '<br/>' . str_repeat('=',60) . '<br/>'; |
|||
|
|||
// -- Get the Lat/Lon of a address ------------------------------------------------------------------------------------- |
|||
$latlong = $geocode->getLatLong('1600 Pennsylvania Ave NW, Washington, DC 20500'); |
|||
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'; |
|||
$destination = '4433 N Indiana Ave, Kansas City, MO 64117'; |
|||
$dist = $geocode->Get_Distance($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 ======================================================================================================= |
Loading…
Reference in new issue