PHP Google Maps API Tutorial

By Nathan Yang

Multiple Markers Demo

Follow the steps below to generate a Google Map using PHP code and adding markers by address.

  1. Download (Right click → Save Link As...) GoogleMap.php
  2. Download (Right click → Save Link As...) JSMin.php
  3. Add the following PHP code to create a 750x500 map with two markers:
<?php
include_once("GoogleMap.php");
include_once("JSMin.php");

$map = new GoogleMapAPI();
$map->setMapType("ROADMAP");
$map->setWidth("750");
$map->addMarkerByAddress("Van Munching Hall, University of Maryland, College Park, MD 20740", "Van Munching Hall", "Van Munching Hall is home to the University of Maryland's Robert H. Smith School of Business. Named after Leo Van Munching, Jr. '50, a Smith School alumnus and president of the company holding the exclusive franchise to import and distribute Heineken and Amstel Light, the building opened in 1993 and has a total space of approximately 240,000 square feet.");
$map->addMarkerByAddress("Theodore R Mckeldin Library, Campus Dr, College Park, MD 20742", "Mckeldin Library", "The main library on the University of Maryland, College Park campus");
?>
<html>
<head>
    <meta charset="utf-8" />
    <title>PHP Google Maps API</title>
<?php
$map->printHeaderJS();
$map->printMapJS();
?>
</head>
<body>
<?php
$map->printOnLoad();
$map->printMap();
?>
</body>
</html>

The addMarkerByAddress function adds a marker to the map by address.

int|bool addMarkerByAddress(string $address, [string $title = ''], [string $html = ''], [string $tooltip = ''], [string $icon_filename = ''], [string $icon_shadow_filename = ''])

Parameters:
string      $address:    the map address to mark (street/city/state/zip)
string      $title:      the title display in the sidebar
string      $html:   the HTML block to display in the info bubble (if empty, title is used)
string      $tooltip:    Tooltip to display (deprecated?)
string      $icon_filename:      Web file location (eg http://somesite/someicon.gif) to use for icon
string      $icon_shadow_filename:   Web file location (eg http://somesite/someicon.gif) to use for icon shadow

You can configure the map by calling specific functions.

More Demos

For more examples of creating Google Maps using PHP code, visit http://www.bradwedell.com/php-google-maps-api/demos.

Credits

The PHP Google Maps API Project