About a month ago Google introduced version 3 of Google Maps Javascript API (V3), a replacement of version 2. A lot of changes under the hood and this version is especially optimized for mobile devices such as Android and the iPhone. Also an interesting note : you don’t need a key anymore!
This new version is implemented using a modified MVC framework. Any state changes of an MVC object (such as a map) for example, are handled through setters and getters of a particular format. As well, all state of the MVC objects are stored as properties of that object, and all observation of state changes through event handlers is of a particular format as well.
But let’s start with a “Hello World”.
Example “Hello World”
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(52.2559251, 6.161336);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
DOC TYPE
Google recommends that you declare a true DOCTYPE within your web application. Within the examples here, we’ve declared our applications as HTML5 using the simple HTML5 DOCTYPE as shown below:
<!DOCTYPE html>
The DOCTYPE is also designed to degrade gracefully; browsers that don’t understand it will ignore it, and use “quirks mode” to display their content.
Note that some CSS that works within quirks mode is not valid in standards mode. In specific, all percentage-based sizes must inherit from parent block elements, and if any of those ancestors fail to specify a size, they are assumed to be sized at 0 x 0 pixels. For that reason, we include the following <style> declaration:
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
Loading the Google Maps API
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
</script>
Displaying the map
And with a simple
<div id="map_canvas" style="width:100%; height:100%"></div>
You displays the map!
Loading the Map
<body onload="initialize()">
And with the event handler ‘onload’ on the body-tag, you’re map will be loaded and displayed!
Next time I’ll show you how you can use geo location.