GMapBuilder - Class for building Google Maps.
1. Author
2. License
3. Description
4. Sites using GMapBuilder
5. Instructions for using GMapBuilder
6. Contact
1. Author
Stephen R. MacMinn
Version 1.0, November 2006.
2. License
GMapBuilder is licensed under the GNU Public License (GPL).
http://www.fsf.org/licensing/licenses/gpl.html#SEC4
3. Description
The GMapBuilder class handles all the messy details of putting fairly complex Google maps on your web site.
GMapBuilder can create Google maps of any size. The maps can have an arbitrary number of markers, each with its own html text block.
Maps can also contain polylines or just about any other Google map construct.
GMapBuilder uses version 1 of the Google Maps API.
3.1 The Internet Explorer Bug
Internet Explorer has a bug which IE cannot manipulate the DOM until the page is fully loaded, when using scripts that create Google maps, this bug may cause the script to fail if the js runs before the map is defined. The only sure way to prevent this is by running the script with the "onload" directive.
GMapBuilder works around this bug by encapsulating all of the required javascript in a javascript function called WaitToRun() and running that function from an "onload" directive in the page <body> tag.
Instructions for using GMapBuilder are below and are also in the class file itself.
IMPORTANT !
Before you can use GMapBuilder, you will need to sign up with Google to obtain a Google Maps API Key. Keys are site
specific. In fact, they're directory specific, meaning that a key will only work in site directory for which you
registered. You can sign up for an API key here: http://www.google.com/apis/maps/signup.html
Google maps require latitude and longitude (geocode) coordinates to map points. For the U.S. there's a geocoding service (translates street address to geocode) at http://geocoder.us . It's free for non-commercial use.
4. Sites using GMapBuilder
If you want to see GMapBuilder in action, here are two sites that use it.
Placewiki: http://www.placewiki.com.
An example of a map with multiple markers is shown at: http://placewiki.com/nearby.php?ref=placeID&placeID=233
PlaceServer: http://stephenmacminn.com/placeserver/index.php
PlaceServer is a little demonstration site I built that uses the U.S. Geologic Survey GNIS (Geographic Names Information System) data to map geographic places in the U.S. Try searching for Lake George
5. Instructions for using GMapBuilder
A google map consists of four basic elements:
1. The <script> tag that loads the javascript API interface from Google. In GMapBuilder this is getGoogleMapAPIScript();
2. The <script> that sets up the map and defines points on it. In GMapBuilder this is getWaitToRunScript();
3. A special <body> tag that keeps all the js from running until after the page has loaded. In GMapBuilder this is getMapBodyTag();
4. The map container: <div id="map">. In GMapBuilder this is getMapDiv();
To use GMapBuilder:
// Instantiate a new GMapBuilder object
$GoogleMapAPIKey = "This is where your big long alphanumeric API key from Google goes";
$map = new GMapBuilder($GoogleMapAPIKey);
// Define the height and width and zoom level. Height and Width are the size of the map div.
$map->setHeight(400);
$map->setWidth(300);
$map->setZoomLevel(6);
// If you want to set styles other than the height and width on the
// map div, use setMapDivStyle() to add them.
$map->setMapDivStyle("float: right; margin-right: 30px;")
// Define the center point of the map
$latitude = 45.789;
$longitude = -75.324;
$map->setCenter($longitude, $latitude);
// Add as many markers as you want to the map
// Each marker has a geocode (lat/lon) and can have some html that will
// appear in a popup when then marker is clicked.
$latitude = 42.892;
$longitude = -75.221;
$html = "<em>This is a point</em>";
$map->addMarker($longitude, $latitude, $html);
$latitude = 42.541;
$longitude = -75.301;
$html = "<em>This is yet another point</em>";
$map->addMarker($longitude, $latitude, $html);
// Add any additional javascript as a string. You can use this e.g., to add polylines to the map.
$map->addAdditionalJS($jsString)
// In the head of the page, output the script that fetches the Google API
echo $map->getGoogleMapAPIScript();
// At the end of head of the page, output the <body> tag.
// If you have other onload functions, you don't need to use this, but be sure to include
// onload = WaitToRun(); in your <body> tag.
echo $map->getMapBodyTag();
// At the location on the page where you want the map, output the map div
echo $map->getMapDiv();
// Output the "WaitToRun" script that contains the actual javascript that creates the map.
echo $map->getWaitToRunScript();
// That's it!
6. Comments and suggestions are welcome: steve at placewiki dot com |