Most of the code from previous Static Maps experiments is now put into one clean package. Previously I showed you how to work with markers and bounds. Now we go forward and add zoom and pan controls. It takes only few lines of code. If you just started reading the series check the theory how it works. As a bonus lets add infowindows / bubbles too.

Note! Image above is just a screenshot. You can test final result in the demo.

Create Map and Some Markers

Start by creating new map object and set the size. We also need to give our API key. Markers are positioned on map using location object. Location can be latitude and longitude represented by Google_Maps_Coordinate object. Location can also be map x and y represented by Google_Maps_Point object.

Because we put markers to map the center is calculated automatically. There is no need to call $map->setCenter(). We can also calculate the closest possible zoom with $map->zoomToFit().

require_once 'Google/Maps.php';

$map = Google_Maps::create('static');
$map->setSize('540x300');
$map->setKey(API_KEY);

$coord_1 = new Google_Maps_Coordinate('58.378700', '26.731110');
$coord_2 = new Google_Maps_Coordinate('58.379646', '26.764090');

$marker_1 = new Google_Maps_Marker($coord_1);
$marker_2 = new Google_Maps_Marker($coord_2);

$map->addMarker($marker_1);
$map->addMarker($marker_2);
$map->zoomToFit();

Add Zoom and Pan Controls

Controls are created using Google_Maps_Control::create() factory method. After creating a control you must attach it to a map. This alone is not enough. When panning and zooming new map center or zoom value is passed in query string. Last line passes the values from URL to the map object.

$zoom = Google_Maps_Control::create('zoom');
$map->addControl($zoom);
$pan = Google_Maps_Control::create('pan');
$map->addControl($pan);

$map->setProperties($_GET);

Note! Image above is just a screenshot. You can test working controls in the demo.

Add Infowindows / Bubbles

Infowindows (or bubbles as they are often referred) are represented by Google_Maps_Infowindow object. You can set the content in constructor or using $bubble->setContent() method. Bubbles have a marker attached to them. Clicking the marker will open the infowindow. As with all other items you must attach them to map.

$bubble_1 = new Google_Maps_Infowindow('Foo Bar');
$bubble_2 = new Google_Maps_Infowindow('Pler pop');

$bubble_1->setMarker($marker_1);
$bubble_2->setMarker($marker_2);

$map->addInfowindow($bubble_1);
$map->addInfowindow($bubble_2);

Note! Image above is just a screenshot. You can test working infobubbles in the demo.

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

6 Responses to “Simple Zoom and Pan Controls With Static Maps”

  1. Alan Evans says:

    This is very nice work. We’ve currently got a map page we want to make a bit more javascript friendly

    http://www.aber.ac.uk/en/maps/penglais/

    One thing we’re doing in the current version though is adding a custom overlay to show our road system and buildings on the map.

    Anyone know if adding this overlay would be possible with the static API?

    Thanks!

  2. Clare says:

    Fantastic work and really good, clear examples. Got this up and running in no time. Thankyou, much appreciated :)

  3. Renzo says:

    Do you mind If I port this to a JAVA/jsp application?

  4. Mika Tuupola says:

    Renzo: Everything is MIT licensed so you are free to do what you want. It is polite to mention the original source though.

  5. Morkus says:

    Hey i really need to do the same thing but 2 my map you know you can look at it at http://www.hurghada.com/map.aspx but i wanna know if i can use those google map navigation buttons only to my map or not i really need help with it and if you know a place to get them i would be really greatfull for it so please if you can help contact me at my e-mail or leave me a post morkus_2@hotmail.com

  6. Warcraft Bot says:

    hrm your website looks quite interesting.. That would be a tough switch however I’m not entirely sure how you might do it Morkus

Leave a Reply



(will not be published)



(you can use Textile for formatting)