Each of the following examples shows only the relevant JavaScript code, not the full HTML file. You can plug the JS code into the skeleton HTML file shown earlier, or you can download the full HTML file for each example by clicking the link after the example.
The following example creates a map and centers it on Prague.
GUnload() will remove most of the circular references that cause memory leaks. You should call GUnload() in the unload event of your page to reduce the potential that your application leaks memory:
The following example displays a map, waits two seconds, and then pans to a new center point.
The panTo method centers the map at a given point. If the specified point is in the visible part of the map, then the map pans smoothly to the point; if not, the map jumps to the point.
You can add controls to the map with the addControl method.
Zoom In
To register an event listener, call the GEvent.addListener method. Pass it a map, an event to listen for, and a function to call when the specified event occurs. In the following example code, we display the latitude and longitude of the center of the map after the user drags the map.
To create an info window, call the openInfoWindow method, passing it a location and a DOM element to display. The following example code displays an info window anchored to the center of the map with a simple "Hello, world" message.
This example uses GMarker to create 3 markers, then addOverlay to display them
To trigger an action when a user clicks the map, register a listener for the "click" event on your GMap2 instance. When the event is triggered, the event handler will receive two arguments: the marker that was clicked (if any), and the GLatLng of the clicked point. If no marker was clicked, the first argument will be null.
"click" event. Other types of overlays, like GPolyline, are not clickable.In the following example, when the visitor clicks a point on the map without a marker, we create a new marker at that point. When the visitor clicks a marker, we remove it from the map.
For more information about events, see the Events Overview. For a complete list of GMap2 events and the arguments they generate, see GMap2.Events.
In this example, we show a custom info window above each marker by listening to the click event for each marker. We take advantage of JavaScript function closures to customize the info window content for each marker.
For more information about events, see the Events Overview. For a complete list of GMap2 events and the arguments they generate, see GMap2.Events.
Version 2 of the API introduces openInfoWindowTabs() and the GInfoWindowTab class to support info windows with multiple, named tabs. The example below displays a simple tabbed info window above a single marker.
This example creates a new type of icon, using the Google Ride Finder "mini" markers as an example. We have to specify the foreground image, the shadow image, and the points at which we anchor the icon to the map and anchor the info window to the icon.
The geocoder can be accessed from within the JavaScript using the GClientGeocoder object. The getLatLng method can be used to convert an address into a GLatLng. Because geocoding involves sending a request to Google's servers, it can take some time. To avoid making your script wait, you need to pass in a function to call after the response comes back. In this example, we geocode an address, add a marker at that point, and open an info window displaying the address.
If you would like to access structured information about an address, GClientGeocoder also provides a getLocations method that returns a JSON object consisting of the following information.
Statusrequest -- The request type. In this case, it is always geocode.code -- A response code (similar to HTTP status codes) indicating whether the geocode request was successful or not. See the full list of status codes.Placemark -- Multiple placemarks may be returned if the geocoder finds multiple matches.address -- A nicely formatted and properly capitalized version of the address.AddressDetails -- The address formatted as xAL, or eXtensible Address Language, an international standard for address formatting.Point -- A point in 3D space.coordinates -- The longitude, latitude, and altitude of the address. In this case, the altitude is always set to 0.Here we show the JSON object returned by the geocoder for the address of Google's headquarters.
In this example, we use the getLocations method to geocode addresses, and extract the nicely formatted version of the address and the two-letter country from the JSON and display it in the info window.
Try these:
Slovansky Dum, Prague, Czech Republic
1600
amphitheatre mtn view ca
1
Telegraph Hill Blvd, San Francisco, CA, USA
4144
Avenue Pierre-De-Coubertin, Montréal, Canada
Champ
de Mars 75007 Paris, France
Piazza
del Colosseo, Roma, Italia
Domkloster
3, 50667 Köln, Deutschland
Plaza
de la Virgen de los Reyes, 41920, Sevilla, España
東京タワー (Tokyo Tower, Tokyo Japan)
123 Main St, Googleville
GClientGeocoder is equipped with a client-side cache by default. The cache stores geocoder responses, so that if the same address is geocoded again, the response will be returned from the cache rather than from Google's geocoder. To turn off caching, pass null to the setCache method of GClientGeocoder. However, we recommend that you leave caching on because it improves performance. To change the cache being used by GClientGeocoder, use the setCache method and pass in the new cache. To empty the contents of the current cache, use the reset method on the geocoder or on the cache directly.
Developers are encouraged to build their own client-side caches. In this example, we construct a cache that contains pre-computed geocoder responses to six capital cities in countries covered by geocoding API. First, we build an array of geocode responses. Next, we create a custom cache that extends a standard GeocodeCache. Once the cache is defined, we call the setCache method. There is no strict checking of objects stored in the cache, so you may store other information (such as population size) in the cache as well.
It is possible to access the Maps API Geocoder using HTTP. However, for any web application
involving users making geocoding requests, this is a phenomenally bad idea, because it will
use your Geocoding quota, and will burn quickly. Please use the GClientGeocoder
for all client-side geocoding.
To access the Maps API geocoder directly using server-side scripting, send a request to http://maps.google.com/maps/geo?
with the following parameters in the URI:
q -- The address that you want to geocode.key -- Your API key.output -- The format in which the output should be generated. The options are xml, kml or json.In this example, we request the geographic coordinates of Google's headquarters.
http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefgIf you specify json as the output, the response is formatted as a JSON object, as shown in the example above. If you specify xml or kml, the response is returned in XML or KML. The XML and KML outputs are identical except for the MIME types.
For example, this is the response that the geocoder returns for "1600 amphitheatre mtn view ca".
If you have a car or motorbike, driving is awesome. If you do not, then it's not so much fun. If you would like your application to display driving directions, then here's how to do it.
KML stands for Keyhole Markup Language. You can learn much more about it at
http://earth.google.com/kml/.
As a very quick overview, it's an industry standard XML variation, used to describe geographic locations. It's
also the common language spoken by the Google Maps API, Google Earth and Google Maps for Mobile.
Google Maps can import external files in a number of ways. The easiest is to import
GeoRSS or KML natively, using GGeoXML
Street View is pretty new (it's still only available in the development version of Google Maps at time of writing).
However, it's also pretty cool. Here's a very quick overview.
Image Cutter allows you to very easily make custom google maps from any image.
You can download it
from the UCL Centre of Spatial Analysis site.
If your code doesn't seem to be working, here are some approaches that might help you solve your problems:
Here are some additional resources. Note that these sites are not owned or supported by Google.