-
Notifications
You must be signed in to change notification settings - Fork 0
/
tectonic_plate_starter_logic.js
147 lines (125 loc) · 4.64 KB
/
tectonic_plate_starter_logic.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Add console.log to check to see if our code is working.
console.log("working");
// We create the tile layer that will be the background of our map.
let streets = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
accessToken: API_KEY
});
// We create the second tile layer that will be the background of our map.
let satelliteStreets = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v11/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
accessToken: API_KEY
});
// Create the map object with center, zoom level and default layer.
let map = L.map('mapid', {
center: [40.7, -94.5],
zoom: 3,
layers: [streets]
});
// Create a base layer that holds all three maps.
let baseMaps = {
"Streets": streets,
"Satellite": satelliteStreets
};
// 1. Add a 2nd layer group for the tectonic plate data.
let allEarthquakes = new L.LayerGroup();
// 2. Add a reference to the tectonic plates group to the overlays object.
let overlays = {
"Earthquakes": allEarthquakes
};
// Then we add a control to the map that will allow the user to change which
// layers are visible.
L.control.layers(baseMaps, overlays).addTo(map);
// Retrieve the earthquake GeoJSON data.
d3.json("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson").then(function(data) {
// This function returns the style data for each of the earthquakes we plot on
// the map. We pass the magnitude of the earthquake into two separate functions
// to calculate the color and radius.
function styleInfo(feature) {
return {
opacity: 1,
fillOpacity: 1,
fillColor: getColor(feature.properties.mag),
color: "#000000",
radius: getRadius(feature.properties.mag),
stroke: true,
weight: 0.5
};
}
// This function determines the color of the marker based on the magnitude of the earthquake.
function getColor(magnitude) {
if (magnitude > 5) {
return "#ea2c2c";
}
if (magnitude > 4) {
return "#ea822c";
}
if (magnitude > 3) {
return "#ee9c00";
}
if (magnitude > 2) {
return "#eecc00";
}
if (magnitude > 1) {
return "#d4ee00";
}
return "#98ee00";
}
// This function determines the radius of the earthquake marker based on its magnitude.
// Earthquakes with a magnitude of 0 were being plotted with the wrong radius.
function getRadius(magnitude) {
if (magnitude === 0) {
return 1;
}
return magnitude * 4;
}
// Creating a GeoJSON layer with the retrieved data.
L.geoJson(data, {
// We turn each feature into a circleMarker on the map.
pointToLayer: function(feature, latlng) {
console.log(data);
return L.circleMarker(latlng);
},
// We set the style for each circleMarker using our styleInfo function.
style: styleInfo,
// We create a popup for each circleMarker to display the magnitude and location of the earthquake
// after the marker has been created and styled.
onEachFeature: function(feature, layer) {
layer.bindPopup("Magnitude: " + feature.properties.mag + "<br>Location: " + feature.properties.place);
}
}).addTo(allEarthquakes);
// Then we add the earthquake layer to our map.
allEarthquakes.addTo(map);
// Here we create a legend control object.
let legend = L.control({
position: "bottomright"
});
// Then add all the details for the legend
legend.onAdd = function() {
let div = L.DomUtil.create("div", "info legend");
const magnitudes = [0, 1, 2, 3, 4, 5];
const colors = [
"#98ee00",
"#d4ee00",
"#eecc00",
"#ee9c00",
"#ea822c",
"#ea2c2c"
];
// Looping through our intervals to generate a label with a colored square for each interval.
for (var i = 0; i < magnitudes.length; i++) {
console.log(colors[i]);
div.innerHTML +=
"<i style='background: " + colors[i] + "'></i> " +
magnitudes[i] + (magnitudes[i + 1] ? "–" + magnitudes[i + 1] + "<br>" : "+");
}
return div;
};
// Finally, we our legend to the map.
legend.addTo(map);
// 3. Use d3.json to make a call to get our Tectonic Plate geoJSON data.
d3.json().then(() {
});
});