PHP code example of arraypress / google-distance-matrix

1. Go to this page and download the library: Download arraypress/google-distance-matrix library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arraypress / google-distance-matrix example snippets


use ArrayPress\Google\DistanceMatrix\Client;

// Initialize client with your API key
$client = new Client( 'your-google-api-key' );

// Using fluent interface
$result = $client
	->set_mode( 'driving' )
	->set_units( 'imperial' )
	->set_avoid( 'tolls' )
	->calculate( 'New York, NY', 'Boston, MA' );

if ( ! is_wp_error( $result ) ) {
	// Get distance and duration
	$distance = $result->get_formatted_distance();
	$duration = $result->get_formatted_duration();

	echo "Distance: {$distance}\n";
	echo "Duration: {$duration}\n";

	// Get raw values in meters/seconds
	$meters  = $result->get_distance_meters();
	$seconds = $result->get_duration_seconds();
}

// Set defaults for multiple calculations
$client
	->set_mode( 'driving' )
	->set_units( 'imperial' )
	->set_language( 'en' );

// Use defaults
$result1 = $client->calculate( 'New York, NY', 'Boston, MA' );

// Override specific options
$result2 = $client->calculate(
	'New York, NY',
	'Boston, MA',
	[ 'mode' => 'transit' ]
);

// Reset to default options
$client->reset_options();

$origins = [
	'San Francisco, CA',
	'Los Angeles, CA'
];

$destinations = [
	'Las Vegas, NV',
	'Phoenix, AZ',
	'San Diego, CA'
];

$result = $client
	->set_mode( 'driving' )
	->set_units( 'imperial' )
	->calculate( $origins, $destinations );

if ( ! is_wp_error( $result ) ) {
	$distances = $result->get_all_distances();
	foreach ( $distances as $route ) {
		echo "From: {$route['origin']}\n";
		echo "To: {$route['destination']}\n";
		echo "Distance: {$route['distance']['text']}\n";
		echo "Duration: {$route['duration']['text']}\n\n";
	}
}

const VALID_MODES = [
    'driving',
    'walking',
    'bicycling',
    'transit'
];

const VALID_UNITS = [
    'metric',
    'imperial'
];

const VALID_AVOID = [
    'tolls',
    'highways',
    'ferries'
];

const VALID_TRAFFIC_MODELS = [
    'best_guess',
    'pessimistic',
    'optimistic'
];

const DEFAULT_OPTIONS = [
    'mode' => 'driving',
    'units' => 'metric',
    'language' => 'en'
];

$result = $client->calculate(
    'New York, NY',
    'Boston, MA',
    [
        'mode' => 'driving',           // driving, walking, bicycling, transit
        'units' => 'imperial',         // metric or imperial
        'avoid' => 'tolls',           // tolls, highways, ferries
        'language' => 'en',           // response language
        'traffic_model' => 'best_guess' // best_guess, pessimistic, optimistic
    ]
);

// Initialize with custom cache duration (1 hour = 3600 seconds)
$client = new Client( 'your-api-key', true, 3600 );

// Results will be cached
$result = $client->calculate( 'New York, NY', 'Boston, MA' );

// Clear specific cache
$client->clear_cache( 'matrix_New York, NY_Boston, MA' );

// Clear all distance matrix caches
$client->clear_cache();