api_key.'&origins='.rawurlencode($origin).'&destinations='.rawurlencode($destination).'&mode=driving&units=imperial&sensor=false * */ public function getDistance($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, $_origin, $_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