3 GoogleMapAPI - A library used for creating google maps.
6 Monte Ohrt <monte [AT] ohrt [DOT] com>
14 require('GoogleMapAPI.class.php');
16 $map = new GoogleMapAPI('map');
17 // setup database for geocode caching
18 $map->setDSN('mysql://USER:PASS@localhost/GEOCODES');
19 // enter YOUR Google Map Key
20 $map->setAPIKey('YOURGOOGLEMAPKEY');
22 // create some map markers
23 $map->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','PJ Pizza','<b>PJ Pizza</b>');
24 $map->addMarkerByAddress('826 P St Lincoln NE 68502','Old Chicago','<b>Old Chicago</b>');
25 $map->addMarkerByAddress('3457 Holdrege St Lincoln NE 68502',"Valentino's","<b>Valentino's</b>");
28 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
29 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
31 <?php $map->printHeaderJS(); ?>
32 <?php $map->printMapJS(); ?>
33 <!-- necessary for google maps polyline drawing in IE -->
34 <style type="text/css">
36 behavior:url(#default#VML);
40 <body onload="onLoad()">
43 <?php $map->printMap(); ?>
45 <?php $map->printSidebar(); ?>
55 View the output of the above example here:
57 http://www.phpinsider.com/php/code/GoogleMapAPI/demo/
62 GoogleMapAPI - A library used for creating google maps using the
63 Google Map public API. Features include multiple map markers, customizable
64 icons, map directions built into info window, and sidebar generation.
66 More information on the Google Map API can be found here:
68 http://www.google.com/apis/maps/
74 Discussions are currently held in the Smarty add-on forum (although Smarty
75 is not necessary to use GoogleMapAPI.)
77 http://www.phpinsider.com/smarty-forum/viewforum.php?f=19
83 GoogleMapAPI($map_id = 'map', $app_id = 'MyMapApp')
84 ---------------------------------------------------
87 The map id is needed only if you have multiple maps on the same page.
88 The App ID is used for the Yahoo GeoCode API, if you use Yahoo for lookups.
89 Use a unique App ID per application.
94 $map = new GoogleMapAPI();
96 // set custom map id and app id
97 $map = new GoogleMapAPI('mymap','myapp');
103 Used to set the database DSN. The database is used to cache
104 geocode lookups (highly recommended!) The PEAR::DB module
105 is required for database access. You will need to create
106 the following database schema (mysql example given):
108 CREATE TABLE GEOCODES (
109 address varchar(255) NOT NULL default '',
110 lon float default NULL,
111 lat float default NULL,
112 PRIMARY KEY (address)
117 $map->setDSN('mysql://DBUSER:DBPASS@DBHOST/DBNAME');
118 $map->setDSN('mysql://geo:foobar@localhost/GEOCODES');
124 Sets the Google Map API Key. This is mandatory, you will need
125 an API Key for your hostname! You can register free here:
127 http://www.google.com/apis/maps/signup.html
131 // enter YOUR registered API Key
132 $map->setAPIKey('ABQIAAAAxp5FF-A0RhHOnnTBwrlRbx');
138 Sets the width of the map window. This can be either px or %.
143 $map->setWidth('500px');
144 $map->setWidth('100%');
150 Sets the height of the map window. This can be either px or %.
155 $map->setHeight('500px');
156 $map->setHeight('100%');
162 This will enable the map controls to zoom/move around the map.
169 This will disable the map controls to zoom/move around the map.
176 This sets the default map zoom level.
180 $map->setZoomLevel(4);
183 setControlSize($size)
184 ---------------------
186 This sets the map control size. Relevant only if map controls are
187 enabled. large = map zoom/move with slider. small = zoom/move without
188 slider. Large is default.
192 $map->setControlSize = 'small';
198 This will enable the map type controls (map/satellite/hybrid).
202 disableTypeControls()
203 ---------------------
205 This will disable the map type controls (map/satellite/hybrid).
212 This sets the default map type. Relevant only if map type controls are
213 enabled. map = default map. satellite = satellite view. hybrid = hybrid
218 $map->setMapType('map'); // default
219 $map->setMapType('satellite');
220 $map->setMapType('hybrid');
226 This enables the map sidebar. Use printSideBar() or getSideBar() to
227 output the sidebar text. Enabled by default.
233 This disables the map sidebar. Enabled by default.
239 This enables map directions in the bottom of the pop-up info window.
246 This disables map directions in the bottom of the pop-up info window.
249 enableZoomEncompass()
250 ---------------------
252 This enables zoom encompass so default map zoom is as small as possible
253 and include all map markers.
256 disableZoomEncompass()
257 ----------------------
259 This disables zoom encompass.
266 Set the map boundary fudge factor. This will adjust how much map
267 boundary encompasses the map markers. (0.01 is default.)
271 $map->setBoundsFudge(0.01);
274 setBrowserAlert($message)
275 -------------------------
277 This sets the alert message that pops up when a browser is not
278 compatible with the Google Map API. Default is:
279 "Sorry, the Google Maps API is not compatible with this browser."
285 This sets the alert message that displays when javascript is disabled.
286 Default: "<b>Javascript must be enabled in order to use Google Maps.</b>"
292 This enables the onLoad() javascript function, which is used by default.
293 This allows the map javascript to be put into the <head></head> tag,
294 then loaded upon page entry with either <body onload="onLoad()"> right
295 in the body tag, or use printOnLoad() to place this elsewhere. With onLoad
296 disabled, the map javascript must be embedded inside the <body></body>
297 tags. Using onLoad() is the best method to use for browser
304 enables info windows on map markers.
311 disables info windows on map markers.
318 enables the map scale.
322 disableScaleControl()
323 ---------------------
325 disables the map scale.
329 enableOverviewControl()
330 -----------------------
332 enables the map overview.
336 disableOverviewControl()
337 ------------------------
339 disable the map overview.
346 This disables the onLoad() javascript function. (see enableOnLoad())
350 setInfoWindowTrigger($type)
351 ---------------------------
353 This sets the info window trigger behavior. click = info window comes up
354 when clicking on a marker. mouseover = info window comes up when mousing
355 over a marker. Default is click.
359 $map->setInfoWindowTrigger('mouseover');
362 addMarkerByAddress($address,$title = '',$html = '')
363 ---------------------------------------------------
365 This adds a marker to the map. The address must be a fully qualified
366 address that can be used by the available geocode lookup services.
368 To add markers with geocodes directly, see addMarkerByCoords().
370 The title is used for the sidebar navigation link. The html is used
371 in the info window that pops up when you click on the map marker. If
372 no html is provided, the title will be used.
374 Note: map centering is done automatically as markers are added.
376 Note: You can use tabbed windows by passing an array of
377 title=>text values as the $html parameter. If driving directions
378 are enabled, they show up under the first tab.
382 $map->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','PJ Pizza','<b>PJ Pizza</b>');
385 addMarkerByCoords($lon,$lat,$title = '',$html = '')
386 ---------------------------------------------------
388 This adds a map marker by geocode (lon/lat) directly. See
389 addMarkerByAddress() for more details.
393 $map->addMarkerByAddress(-96.6539,40.8191,'PJ Pizza','<b>PJ Pizza</b>');
397 addPolyLineByAddress($address1,$address2,$color,$weight,$opacity)
398 -----------------------------------------------------------------
400 This adds a polyline between the two given addresses. You can optionally
401 supply a color (in #ffffff hex notation), weight (thickness in pixels),
402 and opacity (in percentage 0-100).
406 $map->addPolyLineByAddress(
407 '3457 Holdrege St Lincoln NE 68502',
408 '826 P St Lincoln NE 68502','#eeeeee',5,50);
411 addPolyLineByCoords($lon1,$lat1,$lon2,$lat2,$color,$weight,$opacity)
412 --------------------------------------------------------------------
414 This adds a polyline between the two given geocoordinate points. You can optionally
415 supply a color (in #ffffff hex notation), weight (line thickness in pixels),
416 and opacity (in percentage 0-100).
420 $map->addPolyLineByCoords(-96.67,40.8279,-96.7095,40.8149,'#eeeeee',5,50);
423 adjustCenterCoords($lon,$lat)
424 -----------------------------
426 This adjusts the map center coords by the given lon/lat. This is done
427 automatically as you add markers to the map, or you can do it manually
428 with this function. Note: this function is used internally by the
429 library, it isn't normally necessary unless you have a specific need and
430 you know what you are doing.
433 setCenterCoords($lon,$lat)
434 --------------------------
436 This sets the map center coords to the given lon/lat. Center coords are
437 calculated automatically as you add markers to the map, or you reset it
438 manually with this function. Note: this function is used internally by
439 the library, it isn't normally necessary unless you have a specific need
440 and you know what you are doing.
443 setLookupService('GOOGLE')
444 -------------------------
446 This sets the geocode lookup service. Default is GOOGLE, which uses the
447 GOOGLE Geocode API. If you use the YAHOO Geocode API, be sure to set
448 your application ID when instantiating GoogleMapAPI. NOTE: Yahoo API
449 works only for US addresses (AFAIK).
452 setMarkerIcon($iconImage,$iconShadowImage,$iconAnchorX,$iconAnchorY,$infoWindowAnchorX,$infoWindowAnchorY)
453 ----------------------------------------------------------------------------------------------------------
455 This sets the icon image for ALL the map markers. Call this once. If you
456 want to set a different icon for each marker, use addMarkerIcon()
459 You must supply a separate image for the icon and its shadow.
460 iconAnchorX/Y is the X/Y coordinates of the icon for the map point.
461 infoWindowAnchorX/Y is the X/Y coordinates of the icon where the info
464 The iconImage and iconShadowImage can be either fully qualified URLs, or
465 a relative path from your web server DOCUMENT_ROOT. These images MUST
466 exist and readable so the library can calculate the dimensions.
470 $map->setMarkerIcon('/images/house.png','/images/house_shadow.png',0,0,10,10);
473 addMarkerIcon($iconImage,$iconShadowImage,$iconAnchorX,$iconAnchorY,$infoWindowAnchorX,$infoWindowAnchorY)
474 ----------------------------------------------------------------------------------------------------------
476 This sets the icon image for the CURRENT map marker. IMPORTANT: you MUST
477 call addMarkerIcon() once for every marker. Do not use setMarkerIcon()
478 if you use this function.
480 You must supply a separate image for the icon and its shadow.
481 iconAnchorX/Y is the X/Y coordinates of the icon for the map point.
482 infoWindowAnchorX/Y is the X/Y coordinates of the icon where the info
485 The iconImage and iconShadowImage can be either fully qualified URLs, or
486 a relative path from your web server DOCUMENT_ROOT. These images MUST
487 exist and readable so the library can calculate the dimensions.
491 $map->addMarkerIcon('/images/house.png','/images/house_shadow.png',0,0,10,10);
497 This prints the header javascript that goes into the <head></head> tags.
498 To return the header javascript in a variable, use getHeaderJS().
503 <?php $map->printHeaderJS(); ?>
510 This returns the header javascript that goes into the <head></head> tags.
511 To print the header javascript directly, use printHeaderJS().
516 $headerjs = getHeaderJS();
519 <?php echo $headerjs; ?>
526 This prints the map javascript. If onLoad() is enabled, put this in the
527 <head></head> tags, and supply <body onload="onLoad()"> in the body tag
528 (or use printOnLoad()).
530 Otherwise, put this just before the </body> tag to make it load
531 automatically. Note that using onLoad() is recommended over this method.
533 To return the map javascript in a variable, use getMapJS().
538 <?php $map->printMapJS(); ?>
545 This returns the map javascript that goes into the <head></head> tags.
546 To print the map javascript directly, use printMapJS().
551 $mapjs = $map->printMapJS();
554 <?php echo $mapjs; ?>
561 This prints the onLoad() javascript call. This is an alternate to
562 using <body onload="onLoad()"> in the body tag. You can place
563 this anywhere in the html body.
565 To return the onLoad() javascript in a variable, use getOnLoad().
570 <?php $map->printMapJS(); ?>
573 <?php echo $map->printMap(); ?>
574 <?php $map->printOnLoad(); ?>
581 This returns the map javascript that goes into the <head></head> tags.
582 To print the map javascript directly, use printMapJS().
587 $onload = $map->getOnLoad();
588 $mapjs = $map->getMapJS();
589 $map = $map->getMap();
592 <?php echo $mapjs; ?>
596 <?php echo $onload; ?>
604 This prints the map html.
605 To return the map html in a variable, use getMap().
610 <?php $map->printMapJS(); ?>
612 <body onload="onLoad()">
613 <?php echo $map->printMap(); ?>
616 Example 2 (using printOnLoad):
619 <?php $map->printMapJS(); ?>
622 <?php echo $map->printMap(); ?>
623 <?php echo $map->printOnLoad(); ?>
631 This returns the map html in a variable.
632 To print the map html directly, use printMap().
637 $map = $map->getMap();
639 <body onload="onLoad()">
647 This prints the sidebar html.
648 To return the sidebar html in a variable, use getSidebar().
652 <body onload="onLoad()">
656 <?php $map->printMap(); ?>
659 <?php $map->printSidebar(); ?>
669 This returns the sidebar html in a variable.
670 To print the sidbar html directly, use printSidebar().
675 $map = $map->getMap();
676 $sidebar = $map->getSidebar();
678 <body onload="onLoad()">
685 <?php echo $sidebar; ?>
695 This looks up the geocode of an address. It will use the database cache
696 if available. If you want to lookup a geocode without using the database
697 cache, use geoGetCoords(). Note: this function is used internally by the
698 library, it isn't normally necessary unless you have a specific need and
699 you know what you are doing.
703 $geocode = $map->getGeocode('237 S 70th suite 220 Lincoln NE 68510');
705 echo $geocode['lat'];
706 echo $geocode['lon'];
712 This will get the cached geocode info for an address from the database
713 cache, or return FALSE if nothing is available. Note: this function is
714 used internally by the library, it isn't normally necessary unless you
715 have a specific need and you know what you are doing.
719 $geocode = $map->getCache('237 S 70th suite 220 Lincoln NE 68510');
721 echo $geocode['lat'];
722 echo $geocode['lon'];
725 putCache($address,$lon,$lat)
726 ----------------------------
728 This will insert geocode info for the given address into the database
729 cache. Note: this function is used internally by the library, it isn't
730 normally necessary unless you have a specific need and you know what you
735 $map->putCache('237 S 70th suite 220 Lincoln NE 68510',-96.62538,40.812438);
738 geoGetCoords($address)
739 ----------------------
741 This looks up the geocode of an address directly (not from cache.) Note:
742 this function is used internally by the library, it isn't normally
743 necessary unless you have a specific need and you know what you are
748 $geocode = $map->geoGetCoords('237 S 70th suite 220 Lincoln NE 68510');
750 echo $geocode['lon'];
751 echo $geocode['lat'];
753 geoGetDistance($lat1,$lon1,$lat2,$lon2,$unit)
754 ---------------------------------------------
756 This gets the distance between too coordinate points using the great
757 circle formula. $unit can be M (miles),K (kilometers),N (nautical
758 miles),I (inches), or F (feet). Default is M.
762 $distance = $map->geoGetDistance($lat1,$lon1,$lat2,$lon2,$unit);
766 INTEGRATING GOOGLE MAPS WITH SMARTY TEMPLATES
767 ---------------------------------------------
769 Integrating with Smarty is as simple as assigning the necessary vars to the
770 template and then displaying them. Note: This example does not cover how to
771 setup Smarty, please use the Smarty documentation.
776 require('Smarty.class.php');
777 require('GoogleMapAPI.class.php');
779 $smarty = new Smarty();
780 $map = new GoogleMapAPI();
782 // setup database for geocode caching
783 $map->setDSN('mysql://USER:PASS@localhost/GEOCODES');
784 // enter YOUR Google Map Key
785 $map->setAPIKey('YOURGOOGLEMAPKEY');
787 // create some map markers
788 $map->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','PJ Pizza','<b>PJ Pizza</b>');
789 $map->addMarkerByAddress('826 P St Lincoln NE 68502','Old Chicago','<b>Old Chicago</b>');
790 $map->addMarkerByAddress('3457 Holdrege St Lincoln NE 68502',"Valentino's","<b>Valentino's</b>");
792 // assign Smarty variables;
794 $smarty->assign('google_map_header',$map->getHeaderJS());
795 $smarty->assign('google_map_js',$map->getMapJS());
796 $smarty->assign('google_map_sidebar',$map->getSidebar());
797 $smarty->assign('google_map',$map->getMap());
799 // display the template
800 $smarty->display('index.tpl');
803 contents of index.tpl:
804 ----------------------
806 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
807 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
811 <!-- necessary for google maps polyline drawing in IE -->
812 <style type="text/css">
814 behavior:url(#default#VML);
818 <body onload="onLoad()">
821 <td>{$google_map}</td>
822 <td>{$google_map_sidebar}</td>
832 Excellent Google Maps tutorials by Mike:
833 http://www.econym.demon.co.uk/googlemaps/index.htm
835 People who have helped/contributed:
839 Clark Freifeld <clark AT users DOT sourceforge DOT net>
842 drakos7 (from forums)
845 Anyone I missed, drop me a line!
847 Monte Ohrt <monte [AT] ohrt [DOT] com>