Get Google Maps as an Image using Google Static Maps API

Sometimes you want to display Google Map as an image or save Google Map image to the server for further use. Using Google map as an image is comes when we concern about the website loading time. Also, it is useful for displaying the user’s location map in the web application. Instead of calling the Google Maps API all times, just call Google Static Maps API one time and save the location map on the server. The saved Google map image can be shown without calling the Google Maps API.

The Google Static Maps API lets you showing a Google Maps image on the web page without any JavaScript. The Google Static Maps API creates the map based on your given address or latitude-longitude and returns the map as an image.

This tutorial shows the simple way to get Google Map as image based on given address using Google Static Maps API. Also, we’ll provide the simple code to save the Google Maps as an image in the server for further use.

When you want to place a marker in Google Map image, it is required to pass the latitude and longitude with the Google Maps API. Use the following resource to get the Latitude & Longitude from address online.

Google Map Image based on Address

<img src="https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=12&size=600x400"/>

Google Map Image based on Latitude & Longitude

<img src="https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=600x400"/>

Google Map Image with Marker based on Latitude & Longitude

<img src="https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&markers=color:red%7Clabel:C%7C40.718217,-73.998284&zoom=12&size=600x400"/>

Save Google Map as an Image using PHP

<?php
    $src 
'https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&markers=color:red%7Clabel:C%7C40.718217,-73.998284&zoom=12&size=600x400';
    
$time time();
    
$desFolder 'images/';
    
$imageName 'google-map_'.$time.'.PNG';
    
$imagePath $desFolder.$imageName;
    
file_put_contents($imagePath,file_get_contents($src));
?>
<img src="<?php echo $imagePath; ?>"/>

Specify a Google API Key

Use key parameter with the URL of Google Static Maps API for specifying a Google API Key.

https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=600x400&key=YOUR_API_KEY

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

2 Comments

  1. Zadar Said...
  2. Nacef Said...

Leave a reply

keyboard_double_arrow_up