27.10.2014 Views

Google Maps API 3

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 ■ LOCATION, LOCATION, LOCATION<br />

margin-bottom: 10px;<br />

}<br />

#map {<br />

width: 500px;<br />

height: 300px;<br />

border: 1px solid black;<br />

}<br />

The Starting JavaScript<br />

With the HTML and CSS in place, you get to the interesting part, the JavaScript code (Listing 10-3). You<br />

start by laying a foundation. This code will create a regular map centered over the United States.<br />

Listing 10-3. The Starting JavaScript for Example 10-1<br />

(function() {<br />

// Defining some global variables<br />

var map, geocoder, marker, infowindow;<br />

window.onload = function() {<br />

}<br />

// Creating a new map<br />

var options = {<br />

zoom: 3,<br />

center: new google.maps.LatLng(37.09, -95.71),<br />

mapTypeId: google.maps.MapTypeId.ROADMAP<br />

};<br />

map = new google.maps.Map(document.getElementById('map'), options);<br />

// Code for catching the form submit event goes here<br />

// Function stub<br />

function getCoordinates() {<br />

}<br />

})();<br />

Notice that you define some variables at the top of the code. They are defined there since you need<br />

to have access to them from a function that you will create later.<br />

Now that you have the foundation laid out, it’s time to start building the functionality for finding<br />

the position of an address.<br />

Setting Up the Event Handler<br />

The first thing you will do is to set up the event handler for the form. You do this by catching the form’s<br />

submit event. On submit, you take the address from the text input and use it as a parameter for the<br />

function getCoordinates() that you’re going to create in a minute.<br />

214

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!