IT & GIS WebGIS Technical

Six kml files have been uploaded to the webserver.  These files have been created by the author using the ‘snap-to-road’ and ‘export as kml’ features at mapmyride.com and some formatting corrections were then carried out using Google Earth and a text editor.  The latter step included the allocation of colour to each routeline (i.e. each separate file) using the kml style tags.

The co-ordinates are in WGS84.

The kml files represent the six parts of the bicycle route undertaken by the author – for background on the journey see about and please explore the rest of the site too.

The kml is called from the external javascript file googleEarthInitialise.js and addSampleButton.js enables the user interface in the map legend. These javascript are called from html within the MSc page.

Note that whilst viewing the source code of the MSc page in your browser will show the aforementioned scripts (by looking between the body tags for the html in question and by searching for the javascript filenames and then clicking them to display the script itself) it has been listed below too, not least because WordPress set-up adds much additional script to what is essentially a dynamically created html page.


googleEarthInitialise.js…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    var ge;
   
    // store the object loaded for the given file... initially none of the objects
    // are loaded, so initialize these to null
    var currentKmlObjects = {
      '1': null,
      '2': null,
      '3': null,
      '4': null,
      '5': null,
      '6': null
    };
   
    google.load("earth", "1");
   
    function init() {
      google.earth.createInstance('map3d', initCallback, failureCallback);
   
      addSampleUIHtml(
        '<h3>Legend</h3>' + 'Click to toggle<br/>' +
        '<input type="checkbox" id="pt_1_v2" onclick="toggleKml(\'1\');"/><label for="pt_1_v2">Part 1</label><br/>' +
        '<input type="checkbox" id="pt_2_v2" onclick="toggleKml(\'2\');"/><label for="pt_2_v2">Part 2</label><br/>' +
        '<input type="checkbox" id="pt_3_v2" onclick="toggleKml(\'3\');"/><label for="pt_3_v2">Part 3</label><br/>' +
        '<input type="checkbox" id="pt_4_v2" onclick="toggleKml(\'4\');"/><label for="pt_4_v2">Part 4</label><br/>' +
        '<input type="checkbox" id="pt_5_v2" onclick="toggleKml(\'5\');"/><label for="pt_5_v2">Part 5</label><br/>' +
        '<input type="checkbox" id="pt_6_v2" onclick="toggleKml(\'6\');"/><label for="pt_6_v2">Part 6</label><br/>'
      );
    }
   
    function initCallback(instance) {
      ge = instance;
      ge.getWindow().setVisibility(true);
   
      // add a navigation control
      ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
   
      // add some layers
      ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
      ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
   
      // fly to Santa Cruz
      var la = ge.createLookAt('');
      la.set(37, -122,
        0, // altitude
        ge.ALTITUDE_RELATIVE_TO_GROUND,
        0, // heading
        0, // straight-down tilt
        10000000 // range (inverse of zoom)
        );
      ge.getView().setAbstractView(la);
   
      // if the page loaded with checkboxes checked, load the appropriate
      // KML files
      if (document.getElementById('pt_1_v2').checked)
        loadKml('1');
   
      if (document.getElementById('pt_2_v2').checked)
        loadKml('2');
   
      if (document.getElementById('pt_3_v2').checked)
        loadKml('3');
     
      if (document.getElementById('pt_4_v2').checked)
        loadKml('4');
     
      if (document.getElementById('pt_5_v2').checked)
        loadKml('5');
     
      if (document.getElementById('pt_6_v2').checked)
        loadKml('6');
   
      document.getElementById('installed-plugin-version').innerHTML =
        ge.getPluginVersion().toString();
    }
   
    function failureCallback(errorCode) {
    }
   
    function toggleKml(file) {
      // remove the old KML object if it exists
      if (currentKmlObjects[file]) {
        ge.getFeatures().removeChild(currentKmlObjects[file]);
        currentKmlObject = null;
      }
   
      // if the checkbox is checked, fetch the KML and show it on Earth
      var kmlCheckbox = document.getElementById('pt_' + file + '_v2');
      if (kmlCheckbox.checked)
        loadKml(file);
    }
   
    function loadKml(file) {
      var kmlUrl = 'http://www.sladeride.com/msc-data/kml/pt_' + file + '_v2.kml';
   
      // fetch the KML
      google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {
        // NOTE: we still have access to the 'file' variable (via JS closures)
   
        if (kmlObject) {
          // show it on Earth
          currentKmlObjects[file] = kmlObject;
          ge.getFeatures().appendChild(kmlObject);
        } else {
          // bad KML
          currentKmlObjects[file] = null;
   
          // wrap alerts in API callbacks and event handlers
          // in a setTimeout to prevent deadlock in some browsers
          setTimeout(function() {
            alert('Bad or null KML.');
          }, 0);
   
          // uncheck the box
          // document.getElementById('kml-' + file + '-check').checked = '';
        }
      });
    }


addSampleButton.js…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function addSampleButton(caption, clickHandler) {
        var btn = document.createElement('input');
        btn.type = 'button';
        btn.value = caption;
       
        if (btn.attachEvent)
          btn.attachEvent('onclick', clickHandler);
        else
          btn.addEventListener('click', clickHandler, false);

        // add the button to the Sample UI
        document.getElementById('sample-ui').appendChild(btn);
      }
     
function addSampleUIHtml(html) {
        document.getElementById('sample-ui').innerHTML += html;
}


html…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script src="http://www.google.com/jsapi?key=ABQIAAAARNi6MLLlwnUiiLC7YvwkOxTvNcW3ZKPHGqF8xIcWdm_g19f7FxQ5IJXCbNVR4Hvteu-IYbdYRzV9Wg" type="text/javascript"></script>

<script type="text/javascript" src="http://www.sladeride.com/msc-scripts/addSampleButton.js"></script>
<script type="text/javascript" src="http://www.sladeride.com/msc-scripts/googleEarthInitialise.js"></script>

<body onload="init()">
   <table width="100%" border="0" cellspacing="0" cellpadding="3" height="380">
      <tr>
         <td width="80%">
            <div id="map3d" style="width: 500px; height: 380px;"></div>
         </td>
            <td width="20%">
               <div id="sample-ui"></div>
         </td>
      </tr>
   </table>
   <br>
   <div>Installed Plugin Version: <span id="installed-plugin-version" style="font-weight: bold;">Loading...</span>   </div>
</body>

LouiseBrooks theme by Themocracy