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();
// 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();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.