-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipleLayers.html
74 lines (74 loc) · 2.95 KB
/
multipleLayers.html
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
<html>
<head>
<!-- the following links add the CSS and Javascript required for the Leaflet Map -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-
wcw6ts8Anuw10Mzh9Ytw4pylW8+NAD4ch3lqm9lzAsTxg0GFeJgoAtxuCLREZSC5lUXdVyo/7yfsqFjQ4S+aKw=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-
mNqn2Wg7tSToJhvHcqfzLMU6J4mkOImSPTxVZAdo+lcPlk+GhZmYgACEe0x35K7YzW1zJ7XyJV/TT1MrdXvMcA==
"
crossorigin=""></script>
<!-- the following CSS is used to set the size of the Map -->
<style type="text/css">#mapid { height: 180px; }
</style>
<!-- button activation -->
<script>
function loadEarthquakeData() {
alert("Loading Earthquakes");
getEarthquakes();
}
// create a variable that will hold the XMLHttpRequest()
var client;
function getEarthquakes() {
client = new XMLHttpRequest();
client.open('GET','https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson');
client.onreadystatechange = earthquakeResponse;
client.send();
}
// code to wait for the response from the data server, and process the response once it is received
// this function listens out for the server to say that the data is ready
function earthquakeResponse() {
if (client.readyState == 4) {
var earthquakedata = client.responseText;
loadEarthquakelayer(earthquakedata);
}
}
// convert the received data - which is text - to JSON format and add it to the map
function loadEarthquakelayer(earthquakedata) {
var earthquakejson = JSON.parse(earthquakedata);
earthquakelayer = L.geoJson(earthquakejson).addTo(mymap);
mymap.fitBounds(earthquakelayer.getBounds());
}
// second button activation
var earthquakelayer;
function removeEarthquakeData() {
alert("Earthquake data will be removed");
mymap.removeLayer( earthquakelayer );
}
</script>
</head>
<body>
<!-- the mapid div will hold the map -->
<div id="mapid" style="width: 600px; height: 400px;"></div>
<!-- this adds a button -->
<button id="loadData" name="loadData" onclick="loadEarthquakeData()">Click here to load the data </button>
<!-- this adds a second button -->
<button id="removeEarthquakeData" name="removeEarthquakeData"
onclick="removeEarthquakeData()">Click here to remove the Earthquake data </button>
<!-- the following script will load the map and set the default view and zoom, as well as loading the
basemap tiles -->
<script>
// load the map
var mymap = L.map('mapid').setView([51.505, -0.09], 13);
// load the tiles
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data ©<a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>,'+'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(mymap);
</script>
</body>
</html>