Benefits
- Requires some JavaScript knowledge
- Fully customizable
- Interactive maps
Usage
This is the more advanced way of building maps, and it requires a little bit of technical knowledge (especially if you want to build more complex maps), however in return you get a lot more flexibility, customization and most of all maps that can be interactive and dynamically updated if need be.
A good place to start is the tutorial available from Google.
To begin using the JavaScript Google Maps API you need to include the following on your page:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
The sensor parameter like in the static maps should only be true if your website visitors are using a GPS enabled device (e.g. building a website for mobile phone users). Next make sure you have a div in you html code ready to receive the map image.
<div id="map_canvas" style="width: 300px; height: 300px"></div>
The width and height styles do not have to be defined inline, however the div needs to have it's dimensions defined for the map to work.
The map initialization is best done by a javascript function similar to the one below:
<script type="text/javascript"> function initialize() { var latlng = new google.maps.LatLng(-34.397, 150.644); var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); }</script>
Worth noting is that the parameters for the map are similar to the parameters used by the static maps API.
Lastly you need to actually initialize the map once the page is finished loading. A simple way of doing tht is to call the initialize() function in the body tag:
<body onload="initialize()">
Or alternativelly if you are already using jQuery on your website:
<script type="text/javascript">$(function(){ initialize();});</script>
Additional details
The full Google Maps API documentation can be found here.