From 48b38f95010c1a6040f6e4e5f3e60b9c5d7788e8 Mon Sep 17 00:00:00 2001 From: Rick Hays Date: Fri, 27 Sep 2019 13:11:42 -0500 Subject: [PATCH] First Commit, tested out functions. Signed-off-by: Rick Hays --- class/geocode.php | 112 ++++++++++++++++++++++++++++++++++++++++++++ geocode-example.php | 27 +++++++++++ 2 files changed, 139 insertions(+) create mode 100644 class/geocode.php create mode 100644 geocode-example.php diff --git a/class/geocode.php b/class/geocode.php new file mode 100644 index 0000000..9235966 --- /dev/null +++ b/class/geocode.php @@ -0,0 +1,112 @@ +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 + + + + diff --git a/geocode-example.php b/geocode-example.php new file mode 100644 index 0000000..214d7c6 --- /dev/null +++ b/geocode-example.php @@ -0,0 +1,27 @@ +straight_line_distance(39.10968, -94.57949, 39.17521, -94.53957) ; + echo '
' . str_repeat('=',60) . '
'; + +// -- Get the Lat/Lon of a address ------------------------------------------------------------------------------------- + $latlong = $geocode->getLatLong('1600 Pennsylvania Ave NW, Washington, DC 20500'); + echo 'STATUS: ' . $latlong['status'] . '
'; + echo 'LAT: ' . $latlong['lat'] . '
'; + echo 'LON: ' . $latlong['lon'] . '
'; + echo '
' . str_repeat('=',60) . '
'; + +// -- 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'] . '
'; + echo 'DISTANCE: ' . $dist['distance'] . '
'; + echo 'DURATION: ' . $dist['duration'] . '
'; + echo '
' . str_repeat('=',60) . '
'; + +// == END SCRIPT =======================================================================================================