Duncan's blog

September 18, 2011

Google Maps API – an introduction

This is the first in a series of posts I intend to do looking into some uses of the Google Maps Javascript API, V3. Let’s start at the very beginning – you want to display a map on a webpage, so what’s required. This is the bare minimum you need to do.

Add an empty <div> to your HTML page, with a suitable ID attribute:

<div id="map"></div>

Add some CSS like this:

<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map { height: 100% }

Include the Google Maps javascript file:

<script src="http://maps.googleapis.com/maps/api/js"></script>

Create a javascript function that includes all the next few lines of code; let’s stick to convention and call it initialize.

Create a LatLng object which indicates the latitude and longitude where your map is centred:

var latLng = new google.maps.LatLng(51.42838, 0.0); // London

If you don’t already know the latitude and longitude for where you want to place your map, there’s a very easy way to find out:

  1. Go to Google Maps
  2. Search for your desired destination, and zoom right in
  3. Right-click on the map at your destination, and choose the “What’s here?” option
  4. The latitude and longitude coordinates will appear in the search box

Create a structure with the following properties:

var myOptions = {
	zoom: 10,
	center: latLng,
	mapTypeId: google.maps.MapTypeId.ROADMAP

The zoom value can be any integer between 0 (fully zoomed out) and about 18. Some cities will support zoom up to about 20, and there are isolated cases of parts of maps having a zoom level up to 23 or more! In general though you won’t want to zoom in that far. Typically I’d set this around 7 – 15 depending on what kind of map I’m displaying.

The center value uses the LatLng object you already created.

The mapTypeId value can be one of four options:

  1. google.maps.MapTypeId.ROADMAP – normal street map
  2. google.maps.MapTypeId.SATELLITE – satellite photos (might not be available for all locations if you’re zoomed in too far)
  3. google.maps.MapTypeId.HYBRID – combination of a satellite map with streets overlaid on top
  4. google.maps.MapTypeId.TERRAIN – what it sounds like, indicates natural features, height of land etc

I generally stick with ROADMAP, as that’s the most typical map type you see by default. Here’s a demonstration of the different types:

Create a new map with the structure of options you just created, and specifying the DIV where you want the map to appear:

var map = new google.maps.Map(document.getElementById("map"), myOptions);

That’s the end of your initialize function. You could just call this using the onload attribute of the body tag. Or if you were using jQuery, with $(document).ready(). However, Google has an event listener, that does the same thing:

google.maps.event.addDomListener(window, 'load', initialize);

Wrapping it all up, here’s the complete page for simply displaying a map at a given location:

<!DOCTYPE html>
<title>Google Maps API V3 lesson 1</title>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map { height: 100% }

<script src="http://maps.googleapis.com/maps/api/js"></script>

	function initialize() {
		var latLng = new google.maps.LatLng(51.476706,0); // London
		var myOptions = {
			zoom: 15,
			center: latLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		var map = new google.maps.Map(document.getElementById("map"), myOptions);
	google.maps.event.addDomListener(window, 'load', initialize);
	<div id="map"></div>


  1. […] previously showed how to create a very simple map. But usually you’ll want to do more than that. Let’s start with adding markers to the […]

    Pingback by Google Maps API – adding markers « Duncan’s blog — September 25, 2011 @ 12:07 pm | Reply

  2. […] previously I showed how to create a basic map, then how to add markers to it. Now suppose you’re wanting to have one of those little […]

    Pingback by Google Maps API – infowindows « Duncan’s blog — October 8, 2011 @ 12:29 pm | Reply

  3. […] now we’ve created a map, added markers and infowindows. Maybe you want to start drawing lines and shapes on […]

    Pingback by Google Maps API – polylines « Duncan’s blog — February 12, 2013 @ 6:43 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: