So far we have generated a set of example maps, with small incremental changes each time. The code of our page has started to grow; see the source of the map page so far.
If the page is further developed in this way, it will soon start to become
unmanageable. The page mixes together a number of distinct elements:
The page will become much more readable if we start to separate these aspects. The following code shows a re-written version of the page. The behaviour as far as the viewer of the page is the same as the previous version, however there are some important changes to the way that the code is structured. The code below shows the listing of three files:
Code for web page google_eg5.html:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://maps.google.com/maps/api/js">
</script>
<script type="text/javascript" src="map_setup_google_eg5.js"></script>
<link rel="stylesheet" media="all" href="map_style.css" type="text/css">
<title>Google maps: example 5</title>
</head>
<body onload="initialize()">
<h1>Google Maps examples</h1>
<p>[<a href="../index.html">Personal home page</a> |
<a href="index.html">Geog5870 work</a>> ]</p>
<div class="gmap" id="map_canvas" style="height: 400px; width: 600px"></div>
</body>
</html>
Code for Cascading Style Sheet (CSS) file map_style.css:
body {
background-color: white;
color: black;
font-size: small;
font-family: Arial,Helvetica,sans-serif;
}
.gmap {
margin: 5px;
border: thin solid black;
float: right;
}
Code for JavaScript file map_setup_google_eg5.js:
var map; // The map object
var myCentreLat = 53.807767;
var myCentreLng = -1.557428;
var initialZoom = 12;
function initialize() {
var latlng = new google.maps.LatLng(myCentreLat,myCentreLng);
var myOptions = {
zoom: initialZoom,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map =
new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Hello World!"
});
/*
* We can add another marker in a similar manner
* We can create the new LatLng object as we
* need it in the Marker() properties
*/
var marker = new google.maps.Marker({
position: new google.maps.LatLng(53.81,-1.56),
map: map,
title:"And another point"
});
}
The first of these is a simplified version of our previous HTML code example. A number of elements have changed:
map_style.css
. As
written, the file assumes that the style sheet can be found in the same
directory as the HTML file.map_setup_google_eg5.js
– and the line '<script src="map_setup_google_eg5.js"
type="text/javascript"></script>
' is used to load that
file. Again, it is assumed that the file will be found in the same
directory as the HTML file.The second file (the map_style.css
file) is the linked style sheet. It repeats the .gmap
class definition used before, and adds, for the sake of example, a style
definition for the body tag which sets the font style and size. Given the
inheritance rules for Cascading Style Sheets (CSS), changes to the body tag are
inherited by all tags which are found inside the body element unless another command overrides them.
Finally, the third file illustrated is the new map_setup_google_eg5.js
script. Note that we've given it a rather specific name, rather than
something generic that would apply to multiple maps (e.g. map_setup.js
) – which naming model you use
depends on whether we want to use the file with multiple maps (cf. with "map_style.css
" which we want to use with multiple maps).
The file is broadly the same as code embedded in the previous HTML, but has a few notable
differences:
var map;
) has been moved
to the top of the file, outside the initialize()
function. Why
should we do this? You may recall the discussion in the introduction to
JavaScript about variable scope. If the map object is declared inside a
function, it will be visible in that function only. By declaring it outside
any function definition, we make the map object have global scope. If we
add further functions to our code, the global map object will be useable in
those functions as well.var map =
google.maps.Map(...)
, but this has been changed in the revised
example to map = google.maps.Map(...)
. We have already
declared the variable (using var
), so do not need to do so
again.myCentreLat
and
myCentreLng
to hold the centre co-ordinates. This allows us to
leave the map creation part of the script as generalised as possible, and
keep all the per-map configuration together; this is intended to promote
easy re-use of the script.initialZoom
,
which is used in the creation of the Map
object. Again, this
is to keep the map configuration data in a single location.The revised version is an improvement, and should make code re-use and maintenance easier.
Taking your example, split the code up in a similar fashion to the way it has
been done above.
If it doesn't work, can you think why? Check your file names carefully and right click > inspect > console and check for errors.