php save google map image

How to save google map image to server

Once I was assigned a task to make a functionality that saves the google map images to the server. I searched on the internet but was unable to find any existing solution. But then an idea comes to my evil genius mind and I implemented it. Now I am sharing the logic with you all. Here is the code:

<?php
  $gmap = new SplFileObject('google_map.png','w');
  $image = file_get_contents("http://maps.google.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&sensor=false");
  $gmap->fwrite($image);
?>

How It works:

The Google static map API provides a way to get the image by providing the parameters through query strings and google process these parameters and returns an image in png format by default. I called a PHP function file_get_contents to retrieve the image data in variable and wrote it in new image file named “google_map.png”.

That is so simple :)

Note: Google allows its maps for the web browsers only, so displaying google map image to any other thing such as PDF and printing purpose may need special license from Google. For information please read License restrictions at http://code.google.com/apis/maps/terms.html

Tags: , ,

Sunday, October 17th, 2010 PHP 10 Comments