GMap provides a PHP wrapper around the Google Maps Javascript API. Its purpose is to allow a web developer to easily add Google Maps and driving directions to web pages using PHP.
The GMap class is used to create and configure each map (and optional directions) on your page.
If you are creating a map without directions, you must add at least one GMarker object to the GMap. If you add more than one GMarker, the map will be automatically centered and zoomed to display all markers. The GMarker class is also used to set the marker info window contents. The GMap showLocation function will create a GMarker for you. This is useful if you only need one marker and do not need to customize it.
You can add GMarkers to a directions map. Markers will be displayed in addition to the start and end location markers. One or more markers may be added as waypoints to the directions.
The icon associated with each marker can be modified using the GIcon class. You can customize the icon for the entire map (all markers) or customize individual marker icons.
Geocoding is provided by the getLocation function and the GLocation class. These can be used to obtain the latitude and longuitude (as well as city, county, state, postal code, etc.) of any address that is recognized by Google.
gmap_config.php
file:
Set GOOGLE_MAP_API_KEY
to the Google Map API key to you obtained from Google.
examples/index.php
in a browser and view the examples.Any page that contains a map must contain the following:
Specify the height and width of the map in pixels, either in the div tag itself or an external CSS file that is included in the head of the document. For example:
If you do not specify the width, the map will exand to fill its container.
If you have multiple maps on one page, each div id should be unique:
You need PHP code to include the gmap.php
file and create a GMap object.
A location string can be passed to the showLocation function. This will display the map centered on the location with a marker for the location.
Alternately, an array of GMarker objects can be created and passed to the showMap function. This will display a map that is automatically zoomed and centered to include all locations/markers.
In order for a marker to be displayed on a map, it's geocode (latitude/longitude) must be obtained. There is a daily limit on the number of geocode requests per day from any IP address. Exceeding this limit can result in having the IP address temporarily blocked. If the limits continue to be exceeded, the IP address may be blocked permanently.
There are three ways to obtain and set the marker's geocode:
This is the default setting. The browser (client) obtains the geocode for a marker's address using Javacsript. Each request is counted against the browser's IP address.
You can have your server obtain the geocode for each address using PHP. Each request is counted againts the server's IP address.
NOTE: If your server obtains the geocodes, you run the risk of exceeding the daily limits and having your server's IP address blocked, either temporarily or permanently.
If you already know a marker's lat/lon (i.e., from a database), you can specify them:
This method avoids a geocode request in order to display the marker.
Any page that contains Google driving directions must have the following elements:
One div element placeholder for the map and another for the driving directions.
PHP code to include the gmap.php script and create a GMap object. The map and directions are displayed by calling the showDirections function with start and end address strings as arguments.
You can add one or more markers to the driving directions by creating an array of indexes. Each index corresponds to a marker in the GMap's marker array. For example:
will add the first two markers as waypoints to the driving directions.
You can change the language of the directions by setting the locale. The following examples show how to set the locale for some selected languages:
French | $gmap->locale = 'fr'; |
German | $gmap->locale = 'de'; |
Greek | $gmap->locale = 'el'; |
Spanish | $gmap->locale = 'es'; |
Russian | $gmap->locale = 'ru'; |
---|
A Google Bar is a control that allows a user to perform a search for locations within the map. To add a Google Bar to a map:
$gmap = new GMap(); $gmap->enable_google_bar = true;
By default, a user must click on the Google Bar to expand it and display a field for search input. To show the Google Bar as already expanded when it is displayed:
$gmap = new GMap(); $gmap->enable_google_bar = true; $gmap->show_bar_on_load = true;
A map overview is small corner map showing a wider area. To display a map overview:
$gmap = new GMap(); $gmap->show_overview_map_control = true;
Fetch and display ads to the map by creating a GAdsManager object with your publisher ID.
You can display current road traffic information on your map for supported cities.
If you want a button to toggle the trafic overlay on/off, add the following to your page:
For a checkbox:
To display a polyline on your map:
GLatLng objects can be created using a latitude/longitude like above or an address string:
Geodesic lines follow the curvature of the Earth and represent the shortest distance between two points on the surface of the Earth. They appear as curved lines on a flat map. If you want the lines to be displayed as geodesic lines:
The creation of a polygon is similar to a polyline:
Note that the last point is the same as the first point in the array. This closes the polygon.
There are additional settings you can specify including different colors for the edge and fill area and the opacity of each as well. See the GPolygon constructor description for more infomration.
Markers are used to indicate a location on a map. Each marker has a point (latitude and longitude) and an icon. When a user clicks on a marker icon, an info window is displayed.
You can customize the marker icons and info window contents.
Create a new GMarker object, passing in an address string:
You can also pass in a latitude and longitude:
You add the markers to the GMap, by creating an array of one or more
GMarkers and calling the showMap
function:
$markers[0] = new GMarker('New York, NY'); $markers[1] = new GMarker('Boston, MA'); $markers[2] = new GMarker('New Haven, CT'); $gmap = new GMap(); $gmap->showMap($markers);
By default, the marker's info window will display the address string. You can specify an additional title and URL:
$markers[0] = new GMarker('2800 East Observatory Road Los Angeles, CA 90027', 'Griffith Observatory', 'http://www.griffithobservatory.org/');
A hyperlink will be created using the title and URL, followed by the address string.
To specify the entire contents of the marker info window, create a string containing the HTML you want displayed. Be sure to use the addslashes function when setting the marker info:
$marker = new GMarker('New York, NY'); $html = '<p>This is some HTML</p>'. '<p>This is some more HTML</p>'; $marker->info = addslashes($html);
To change the default icon for all markers on the map, create a new GIcon object, specifying the URL to the image you want to use, along with its width and height in pixels, and pass it to the GMap object:
To change the default icon for a specific marker on the map, set the icon on the GMarker object, instead of the GMap:
You can make your markers draggable by the user. For example:
If you define a Javascript function
called markerDragUpdate(marker, point)
, this function will be called as the marker is dragged,
giving you access to the marker's current latitude and longitude.
The marker argument is a Google Map API GMarker object
and the point argument is a Google Maps API GLatLng object.
For example, the following function will update a form with the marker's title and current latitude and longitude:
<script language="javascript" type="text/javascript" defer> <!-- function markerDragUpdate(marker, point) { document.myForm.title.value = marker.getTitle(); document.myForm.lat.value = point.lat(); document.myForm.lon.value = point.lng(); } // --> </script> <form name="myForm"> Title <input type="text" name="title"> Lat <input type="text" name="lat"> Lon <input type="text" name="lon" value=""> </form>
You can create hyperlinks that will display a marker's info window using the markerLink function. For example:
This will create a hyperlink to marker zero's info window.
Use the getLocation() function to obtain geocode data for any address string recognized by Google as a valid address.
To geocode form input, create a form with an address field and hidden lat/lon fields:
<form method="post" action=""> Enter Address <input type="text" name="address" size="80" onKeyPress="return checkAddressEnter(this.form,event)"> <input type="hidden" name="lat"> <input type="hidden" name="lon"> <input type="submit" value="Submit" onClick="return geocodeForm(this.form);"> </form>
Include the necessary form-processing javascript by calling the code322_address_form_js() function.
When the user clicks submit, the Javascript will obtain the latitude and longitude of the entered address and set the hidden values of the form. It will then submit the form to the server.
By default, an alert window will be displayed if an occurs while attemping to display a map. You can ovveride this behavior and handle errors yourself, by turning off error handling and checking for errors in your code. Google Map API key errors will still be displayed in alert windows.
To turn off error handling:
$gmap->handle_errors = false;
Then check for errors after calling showLocation() or showMap():
if ($gmap->error) { foreach ($gmap->error_msg as $current) { echo $current; } }
For directions, you can check each location using the getLocation() function before calling showDirections().
$fromLocation = getLocation($from); if ($fromLocation->error) { echo 'Error: Code '.$fromLocation->code.' '.$fromLocation->status; }
Edit the gmap_config.php
file and set:
Specifies settings for AdSense ads on the map.
GAdsManager($publisher_id[, $channel[, $max_ads_on_map[, $min_zooom_level]]])
$publisher_id | String containing the site's AdSense account. |
---|---|
$channel | Optional string containing the AdSense channel used for fetching ads. |
$max_ads_on_map | Optional integer specifying the maximum number of ads to display on the map. Default is 3. |
$min_zooom_level | Optional integer specifying the minimum map zoom level required to display ads. Default is 6. |
$enabled | Boolean to enable (true) or disable (false) the display of ads. Initial setting is true. |
---|
This class contains information used to override the default marker icon.
GIcon($image[, $width[, $height]])
$image | URL to the image. |
---|---|
$width | Optional width in pixels of the icon. If not specified, 12 is used. |
$height | Optional height in pixels of the icon. If not specified, 20 is used. |
$anchor_x | The x pixel coordinate relative to the top left corner of the icon image at which the icon is acnhored to the map. |
---|---|
$anchor_y | The y pixel coordinate relative to the top left corner of the icon image at which the icon is anchored to the map. |
$image | URL to the icon image. |
$info_anchor_x | The x pixel coordinate relative to the top left corner of the icon image at which the info window is achnored to the icon. |
$info_anchor_y | The y pixel coordinate relative to the top left corner of the icon image at which the info window is achnored to the icon. |
$height | Height in pixels of the icon image. |
$width | Width in pixels of the icon image. |
This class contains a latitude and longitude. Arrays of GLatLng objects are used to create GPolyline and GPolygon objects.
GLatLng($arg1[, $agr2])
$arg1 | This contains either an address string or a latitude. |
---|---|
$arg2 | Optional argument that contains a longitude. If specified, $arg1 is assumed to be a latitude, not an address string. |
$lat | The latitude. |
---|---|
$lng | The longitude. |
This class contains the data returned from the getLocation function.
$street | The street address of the location. |
---|---|
$city | The city of the location. |
$county | The county of the location. |
$state | The state/province of the location. |
$postal_code | The postal code of the location. |
$country | The country of the location. |
$lat | The latitude of the location. |
$lon | The longitude of the location. |
$code | The status code returned from the geocode request. |
$status | The status message corresponding to the status code. |
This class is used to create and configure a Google map.
GMap([$map_div[, $dir_div[, $api_key]]])
$map_div | Optional name of the map div in which to display the map. If not specified, "gmap" will be used. |
---|---|
$dir_div | Optional name of the directions div in which to display the driving directions. If not specified, "dir" will be used. |
$api_key | Optional Google Map API key. If not specified, the one in the config file is used. |
$ads_manager | GAdsManager used to display AdSense ads on the map. |
---|---|
$enable_dragging | Boolean to turn map dragging on/off. Default is true (on). |
$error | Boolean to indicate that an error occurred. |
$error_msg | Array of string containing error messages. |
$icon | GIcon to use for all markers, instead of the default Google Map Icon. |
$handle_errors | Boolean to control how errors are handled. If true (default) errors are displayed in an alert window. |
$key | The Google Map API Key to use. If not set, the one in the config file is used. |
$locale | String containing the locale ("fr","es", etc.) |
$map_blowup_type |
String that contains the initial map type displayed (map, satellite or hybrid) for blowups. Valid values are 'map','sat', or 'hybrid'. Default is 'map'. |
$map_blowup_zoom | Integer containing the initial zoom value of the map blowups. |
$map_type |
String that contains the initial map type displayed (map, satellite or hybrid). Valid values are 'map','sat', or 'hybrid'. Default is 'map'. |
$markers | An array of GMarker objects to display. |
$show_map_blowup | Boolean to turn the map type blowups on/off. When this is on, clicking a map marker will display a zoomed-in map in the info window. Default is false (off). |
$show_map_control | Boolean to turn map pan/zoom control on/off. Default is true (on). |
$show_map_type_control | Boolean to turn the map type (Map/Satellite/Hybrid) control on/off. Default is true(on). |
$show_markers | Boolean to turn location markers on/off. Default is true (on). |
$traffic_overlay | Boolean to turn traffic information overlay on/off. Default is false (off). |
$waypoints | An array of integers, each of which is the index of a marker in the marker array ($markers). Each marker that is listed in this array will be added as a waypoint in the driving directions. |
$zoom | Integer containing the initial zoom value of the map. Default value is 13. |
showDirections($loc1, $loc2)
Creates a Google map and directions for the specified locations.
$loc1 | String containing the start address. |
---|---|
$loc2 | String containing the end address. |
showLocation($addr, $title='', $url='')
Displays the specified address in a map.
$addr | String containing an address or lat/lon. |
---|---|
$addr | Optional title string to display in the location's marker. |
$addr | Optional url to display in the location's marker (as a hyperlinked title). |
showMap($markers)
Displays a Google map and creates markers for the specified locations.
$markers | Array of one or more GMarker objects. |
---|
This class contains display information for a marker in a Google map.
GMarker($address[, $name[, $url]])
$address | String containing either an address or a latitude,longitude. |
---|---|
$name | Optional string containing the name/title of the marker. |
$url | Optional string containing a URL for the marker. Used to hyperlink the name/title. |
$icon | Optional GIcon to use for this marker, instead of the default Google Map Icon. |
---|---|
$image | Optional string containing the path to an image file. The image will be displayed in the marker. |
$info | Optional string containing HTML to override the default information displayed in the marker. |
$lat | Latitude of the marker location. If not specified, latitude will be determined using Google's geocoding service. |
$lon | Longitude of the marker location. If not specified, longitude will be determined using Google's geocoding service. |
$map_blowup_type | Map type of marker blowup ('map','sat' or 'hybrid'). |
$map_blowup_zoom_level | Initial zoom level of map blow-up. |
$name | Optional string containing the name of the location. Used as the title in map marker display. |
$show_map_blowup | If true, the marker will contain a map-blowup instead of an info window. Defaults to false. |
$url | Optional string containing a URL, used in with $name to create a hyperlinked title in the map marker. |
Specifies a polyline map overlay.
GPolyline($points, $color, $weight)
$points | An array of GLatLng objects. |
---|---|
$color | The color of the line, specified as hex, i.e., "#ff0000" . |
$weight | Integer specifying the thickness of the line. |
Specifies a polygon map overlay.
GPolygon($points, $edge_color[, $weight[, $fill_color[, edge_opacity[, fill_opacity]]]])
$points | An array of GLatLng objects. |
---|---|
$edge_color | The color of the outline/edge, specified as hex, i.e., "#ff0000" . |
$weight | Optional integer specifying the thickness of the line. Default is 3. |
$fill_color | Optional fill area color (hex string). If not specified, the edge color is used. |
$edge_opacity | Optional number between 0 and 1 specify the edge opacity. Default is 1. |
$fill_opacity | Optional number between 0 and 1 specify the edge opacity. Default is 0.2. |
code322_address_form_js($key = '')
This function generates the Javascript needed for a geocode address field.
$key | Optional Google Maps API key. If not specified, the one in the config file will be used. |
---|
getLocation($address, $key='')
This function returns a GLocation object for the specified address.
$address | A string containing an address. |
---|---|
$key | Optional Google Maps API key. If not specified, the one in the config file will be used. |
markerLink($num, $text, $div='gmap')
This function generates a string containing HTML for a hyperlink to a marker info window. When the link is clicked, the specified marker's info window is displayed.
$num | Marker number, corresponds to the marker's index in the GMap->marker array. |
---|---|
$text | String containing the text of the hyperlink. |
$div | Optional name of the div containing the map. If not specified, "gmap" is used. |