Computer Science 180
Web Design
Fall 2011, Siena College
This week's lab will introduce you to the Google Maps API, a free and fun way to get maps, just like the ones you see when you go to http://maps.google.com, into your own documents.
You may work individually or with a partner on all parts of this lab.
There are a series of questions you need to answer and turn in by email. You may wish to open an email draft or a Word document to record your answers as you go.
Google Maps API Basics
Google has made an application programmer interface (API) available to allow customized plots of data in Google Maps. An API is a set of mechanisms that allow us to create and interact with some data, in this case a Google Map. We will work this week with the JavaScript-based API, meaning we will create and modify our maps using JavaScript code.
We will start with an example that simply displays a map and allows us to navigate around using the familiar Google Maps controls. Copy this example to your folder for this week's lab. Load it into your browser and make sure you see a map.
A couple more items of note:
<script>
tag. This is done here
to access all of the JavaScript code that makes up the Google Maps
API.
<body>
tag executes the
given JavaScript function when the page first loads. This gives us
a way to create the map in JavaScript code as soon as the page
loads.
Now, we will make some modifications to the initialize function.
In the first line of the initialize function, we create a LatLng, which as you may guess, represents a latitude, longitude pair. This will be used to set the center of the map when it is first displayed. Latitude values are positive for north latitude, negative for south latitude. Longitude values are positive for east longitude, negative for west longitude.
Next, change the value of the zoom option on the third line of the initialize function to a few other values in the range 0-18 and reload the page after each change.
<div>
that is intended to take up the entire window.
One more experiment before we start adding things to our maps. Change
the onload= in the <body>
tag to be onclick=.
<div>
element. It will go away when the map is created (the
map will replace the contents of the <div>
). We also need to
make sure we only load the map once. To accomplish this, we'll add a
global variable just before the initialize function inside the
second <script>
element:
var mapLoaded = false;
Then, in the body of the initialize function, we add two lines:
if (mapLoaded) return; mapLoaded = true;
This will make sure that we remember when the map is created (by changing mapLoaded to true), and any subsequent mouse clicks will still call initialize but will not cause the map to be redrawn.
Save this version of the document as clicktoload.html. You need not link to it from your main index, but you should include it in your email submission of this lab.
Combining Maps with Other Elements
Make a copy of the second starter example in your folder for this lab. We will add some new elements to the HTML to combine the map with some other information.
The starting document has some style attributes and a new <div>
before the map that gives us a place to add other HTML to the document
that will be displayed above the map.
Within this <div>
, create a form that displays the following:
When the button is pressed, your form should call a JavaScript function (mine is called recenter). This function will read the values from the two text inputs as a latitude and longitude pair, and if they are valid, recenter the map at that position.
Your form's two text inputs should be validated to require a value between -90 and 90 for latitude, -180 and 180 for longitude. Note that a function is provided in the starter document that will determine if a string represents a valid number in a given range. You may use any of the techniques we have seen this semester to report validation errors, so long as there is some feedback and the map does not recenter unless the inputs are valid.
Once you have validated the new latitude and longitude, you should recenter the map. We will use two steps:
var newpos = new google.maps.LatLng(newlat, newlng);
map.setCenter(newpos);
The remaining task is to have the line below the inputs and button display the current center of the map's view at all time. This should happen not only when the map is recentered by our button, but whenever someone interacts with the map itself in a way that changes its center coordinates.
Here are the tools you will need to accomplish this:
google.maps.event.addListener(map, 'center_changed', updatecenter);
This will call the function updatecenter each time the center of the map is changed for any reason.
map.getCenter();
This can then be used as appropriate - in our case we simply set this as the innerHTML of your message element.
Add a link to this document on the main index of each group member and include this document in your email submission.
Adding Overlays
There are several types of map overlays supported by the Google Maps API. These allow us to add some information about locations and routes on top of an existing map view. The overlays we will consider are markers that are placed at specific locations on the map, polylines that are lines connecting a collection of locations, and info windows that can display additional information in a popup "balloon" with text and/or images.
Start with this document.
The first item we will add is a marker. Add the following code to the bottom of your initialize function:
var marker = new google.maps.Marker({ position: latlng, map: map, title:"Siena College" });
Next, add the following after the code you added in the previous step:
var sienaInfo = '<div><h3>Siena College</h3>' + '<p>This is <a href="http://www.siena.edu">' + 'Siena College</a></p></div>'; var infowindow = new google.maps.InfoWindow({ content: sienaInfo }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); });
Then reload your page.
You have created an info window.
More Markers and Info Windows
Now, add at least two more markers with appropriate info windows, at least one of which is within a few miles of Siena. The steps to add each of these:
For each, choose new variable names as needed for the objects you are creating.
Polylines
We will next add a path to the map connecting two of your markers. I called two of my markers latlng and latlng2, so the following adds the path:
var path = [ latlng, latlng2 ]; var poly = new google.maps.Polyline({ path: path }); poly.setMap(map);
Add similar code, using the names you chose for two of your markers.
We have some control over how this path will be rendered. Change your Polyline construction to specify some options:
var poly = new google.maps.Polyline({ path: path, strokeColor: "#800080", strokeOpacity: 0.4, strokeWeight: 6 });
Choose values that you think make your line look nice.
Your last task is to augment this path to include some intermediate points to make it roughly follow roads between your markers.
To do this, determine the latitude, longitute coordinates of a set of at least three additional points along a reasonable driving route between your markers. For each one, create a new google.maps.LatLng and give it a name. If your intermediate points are named waypoint1, waypoint2, and waypoint3, the path definition would become:
var path = [ latlng, waypoint1, waypoint2, waypoint3, latlng2 ];
Add a link to this document on the main index of each group member and include this document in your email submission.
Submitting Your Work
Before 1:30 PM, Monday, November 28, 2011, submit your files for grading. There are three things you need to do to complete the submission: (i) make sure your pages are uploaded properly to the class web space and are available at your URI, (ii) update your main index on the class web space to have links to the documents you have created for this lab, and (iii) email a copy of your files to me at jteresco AT siena.edu. If you worked with a partner, you need only upload to the account of one of the team members, but each team member should link to the page on his or her own main index. Make sure your pages validate properly as XHTML Strict.
Grading Sheet
This lab is worth 30 points, which are distributed as follows:
> Feature | Value | Score |
Required Components | ||
Lab questions | 6 | |
clicktoload.html | 2 | |
mapbrowse.html form and validation | 2 | |
mapbrowse.html map recenters correctly | 2 | |
mapbrowse.html map center always displayed | 2 | |
plotroute.html first marker | 1 | |
plotroute.html first info window | 1 | |
plotroute.html additional markers and info windows | 4 | |
plotroute.html basic polyline | 2 | |
plotroute.html polyline path between 2 markers | 3 | |
Style | ||
XHTML document passes validation | 2 | |
Formatting of HTML text | 1 | |
Submission | ||
Main index updated | 1 | |
Email submission | 1 | |
Total | 30 | |