Simple Static Maps With PHP
October 10th, 2008
Lately I have been playing with Google Static Maps API a lot. Writing the same things again and again is tedious job. I decided to put the code together as one clean extendable package. Writing object oriented interface for generating URL is trivial. Real meat is having working zoom and pan controls on static map with just 9 lines of code (demo now includes also clickable markers and infowindows).
Code is still alpha quality. API might change any time. But here is a quick walkthrough of current features. We will build the map you see above step by step.
Create a Map Object
Map object is created using Google_Maps::create(‘static’) factory method. If no markers are set you also need to set the center of the map.
require_once 'Google/Maps.php';
$map = Google_Maps::create('static');
$map->setSize('540x300');
$map->setCenter(new Google_Maps_Coordinate('58.368488', '26.768908'));
$map->setZoom(8);
$map->setKey(API_KEY);
Add some markers
Location on map can be in two ways. Latitude and longitude represented by Google_Maps_Coordinate object. Or pixel x and pixel y location represented by Google_Maps_Point object. You can use both when creating a marker.
$coord_1 = new Google_Maps_Coordinate('58.378700', '26.731110');
$coord_2 = new Google_Maps_Coordinate('58.368488', '26.768908');
$coord_3 = new Google_Maps_Coordinate('58.268488', '26.768908');
$marker_1 = new Google_Maps_Marker($coord_1);
$marker_2 = new Google_Maps_Marker($coord_2);
$marker_3 = new Google_Maps_Marker($coord_3);
$marker_1->setColor('green');
$marker_2->setColor('blue');
$marker_3->setColor('orange');
$map->setMarkers(array($marker_1, $marker_2, $marker_3));
Automatically Calculate Zoom and Center
Google Static Maps API can automatically calculate zoom and center for you. However there is no way to know what zoom level it chose. If you need to know automatically calculated zoom and center use $map->zoomToFit() method.
Here we also add new marker using pixel coordinates. Note that we also clear the center of the map we set in the beginning. This allows map to recenter the map according to markers on the map.
$point_1 = new Google_Maps_Point('308107197', '160958681');
$marker_4 = new Google_Maps_Marker($point_1);
$map->setCenter(false);
$map->addMarker($marker_4);
$map->zoomToFit();
Show Marker Bounds
Sometimes you need to be able to visually see bounding box where all the markers fit in.
$map->showMarkerBounds();
Show Map Bounds at Chosen Zoom Level
You can also calculate and show map bounds at any zoom level. Example below displays map bounds at zoom level 8. Map itself is at zoom level 7.
$map->setZoom(7); $map_bounds = $map->getBounds(8); $map->setPath($map_bounds->getPath());
Where’s the Source?
If you want to play around with code you can get from github. Patches, improvements and suggestion are welcome.
git clone git://github.com/tuupola/php_google_maps.git
wget http://github.com/tuupola/php_google_maps/zipball/master
Related entries: Infowindows With Google Static Maps, Clickable Markers With Google Static Maps.

October 14th, 2008 at 02:40 PM
Hi Mike,
been following your Google Maps related blogposts (and the others too of course) for quite some time now. Very nice work I must say!
Regards, B!
October 15th, 2008 at 11:43 AM
Thanks :) Keep your eye on the Static Maps class. I should be able to finish infowindow / infobubble support during this weekend. If you have any suggestions let me know.
October 17th, 2008 at 07:22 PM
Hi Mike, great work and fine code in your google maps classes. Now it’s easy to get the right hot spots on a static map.
Greetings from Germany
October 18th, 2008 at 12:04 PM
Sunfish: Glad to hear. If you have any example links please post them here (or send me an email). I would be interested in seeing how people are using the code.
October 24th, 2008 at 10:06 AM
Can i provide zoom in or/and zoom out functionality, for static google map?
October 24th, 2008 at 04:20 PM
msm: Quote from beginning of this blog entry “Real meat is having working zoom and pan controls on static map”. So answer is yes. You can see it working at the demo page.
That page was recently updated to have markers and infowindows too. These are all done without using any JavaScript.
October 30th, 2008 at 01:40 AM
Very nice class. The version I downloaded had an issue with printing html entities without ; at the end…easily fixed. The layout of the info bubbles is also a bit off but that’s fine too (I read the disclaimer:). Actually this doesn’t both me since I’m thinking of using the click-on-marker, to make the map center and zoom…perhaps display the bubble info outside of the image as simple text. I’m having so much fun with this class, makes it very easy to deliver maps for browsers (like mobile) that dont have full JS/CSS support. Very nice work.
October 30th, 2008 at 06:06 PM
mike z: Thanks! When you publish something send me a link. I would be interested in seeing what people are doing.