PHP code example of arraypress / google-maps-static

1. Go to this page and download the library: Download arraypress/google-maps-static 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-maps-static example snippets


use ArrayPress\Google\MapsStatic\Client;

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

// Configure options using method chaining
$client
	->set_size( 800, 600 )
	->set_zoom( 15 )
	->set_map_type( 'roadmap' )
	->set_format( 'png' )
	->set_scale( 2 )
	->set_language( 'en' )
	->set_region( 'US' );

// Generate a basic map for a location
$map_url = $client->location( 'Seattle, WA' );
$img_tag = $client->generate_image_tag( $map_url );

// Create a map with markers
$markers = [
	[
		'style'     => [ 'color' => 'red', 'label' => 'A' ],
		'locations' => [ 'Seattle, WA' ]
	]
];
$map_url = $client->markers( $markers );

// Generate a map with a custom path
$path_points = [ 'Seattle, WA', 'Portland, OR' ];
$map_url     = $client->path( $path_points );

// Map dimensions and display
$client->set_size( 800, 600 );         // Set width and height
$client->set_zoom( 15 );               // Set zoom level (0-21)
$client->set_map_type( 'satellite' );  // roadmap, satellite, terrain, hybrid
$client->set_format( 'png' );          // png, jpg, gif
$client->set_scale( 2 );               // 1, 2, or 4

// Localization
$client->set_language( 'en' );         // Language code
$client->set_region( 'US' );           // Region code

// Street view settings
$client->set_heading( 90 );            // 0-360 degrees
$client->set_pitch( - 30 );            // -90 to 90 degrees

// API management
$client->set_api_key( 'new-key' );     // Update API key
$client->reset_options();              // Reset to defaults

// Map configuration
$dimensions = $client->get_size();      // Returns ['width' => int, 'height' => int]
$zoom       = $client->get_zoom();      // Current zoom level
$type       = $client->get_map_type();  // Current map type
$format     = $client->get_format();    // Current image format
$scale      = $client->get_scale();     // Current scale factor

// Localization settings
$language = $client->get_language();    // Current language code
$region   = $client->get_region();      // Current region code

// Street view settings
$heading = $client->get_heading();      // Current heading in degrees
$pitch   = $client->get_pitch();        // Current pitch in degrees

// API and options
$key     = $client->get_api_key();      // Current API key
$options = $client->get_options();      // All current options

// Configure and generate map URL
$map_url = $client
	->set_size( 800, 600 )
	->set_zoom( 14 )
	->set_map_type( 'roadmap' )
	->location( 'Seattle, WA' );

// Save to media library with custom settings
$attachment_id = $client->save_to_media_library( $map_url, [
	'title'       => 'Seattle Downtown Map',
	'filename'    => 'seattle-downtown',
	'description' => 'Static map of downtown Seattle',
	'alt'         => 'Map showing downtown Seattle area',
	'folder'      => 'google-maps/seattle'
] );

if ( ! is_wp_error( $attachment_id ) ) {
	// Get the attachment URL
	$image_url = wp_get_attachment_url( $attachment_id );

	// Use WordPress image functions
	echo wp_get_attachment_image( $attachment_id, 'full', false, [
		'class'   => 'my-map-image',
		'loading' => 'lazy'
	] );
}

$client
	->set_size( 800, 600 )
	->set_zoom( 14 )
	->set_map_type( 'roadmap' );

// Single marker
$markers = [
	[
		'style'     => [
			'color' => 'red',
			'size'  => 'mid',
			'label' => 'A'
		],
		'locations' => [ 'Seattle, WA' ]
	]
];

// Multiple markers with different styles
$markers = [
	[
		'style'     => [ 'color' => 'blue', 'label' => 'S' ],
		'locations' => [ 'Space Needle, Seattle' ]
	],
	[
		'style'     => [ 'color' => 'green', 'label' => 'P' ],
		'locations' => [ 'Pike Place Market, Seattle' ]
	]
];

$map_url = $client->markers( $markers );

$client
	->set_size( 800, 400 )
	->set_map_type( 'roadmap' );

// Simple path
$path_points = [ 'Seattle, WA', 'Tacoma, WA', 'Olympia, WA' ];

// Path with styling
$map_url = $client->path( $path_points, [
	'path_style' => [
		'weight'   => '5',
		'color'    => 'blue',
		'geodesic' => 'true'
	]
] );

// Custom map styling
$styles = [
	[
		'feature' => 'water',
		'element' => 'geometry',
		'rules'   => [
			'color' => '0x2c4d58'
		]
	],
	[
		'feature' => 'landscape',
		'rules'   => [
			'color' => '0xeaead9'
		]
	]
];

$map_url = $client
	->set_size( 800, 600 )
	->set_zoom( 12 )
	->styled( $styles, [ 'center' => 'Seattle, WA' ] );

// Configure street view parameters
$map_url = $client
	->set_heading( 180 )    // Face south
	->set_pitch( 20 )      // Look slightly upward
	->set_zoom( 1 )        // Close-up view
	->location( 'Space Needle, Seattle' );