-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
137 lines (111 loc) · 4.41 KB
/
logic.js
File metadata and controls
137 lines (111 loc) · 4.41 KB
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
// Store our API endpoint inside queryUrl
var earthquake_url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson";
// Perform a GET request to the earthquake_url
d3.json(earthquake_url, function(data) {
// Once we get a response, send the data.features object to the createFeatures function
createFeatures(data.features);
});
function createFeatures(earthquakeData) {
// Create a GeoJSON layer containing the features array on the earthquakeData object
// Run the onEachFeature function once for each piece of data in the array
var earthquakes = L.geoJSON(earthquakeData, {
onEachFeature: function(feature,layer){
layer.bindPopup("<h3> Location:" + feature.properties.place +
"</h3> <h3> Magnitude:" + feature.properties.mag +
"</h3><hr><p>" + new Date(feature.properties.time) + "</p>");
},
pointToLayer: function(feature, latlng){
return new L.circle(latlng,
{ radius: dataMarker(feature.properties.mag),
fillColor: dataColor(feature.properties.mag),
fillOpacity: .8,
color: 'grey',
stroke: true,
weight: .5
})
}
});
// Sending our earthquakes layer to the createMap function
createMap(earthquakes);
}
// Scale the data marker circle based on magnitude
function dataMarker(magnitude) {
return magnitude * 20000;
};
// Color the data marker circle based on magnitude
function dataColor(magnitude) {
if (magnitude > 4) {
color = "red";
} else if (magnitude > 3){
color = "green";
} else if (magnitude > 2){
color = "yellowgreen";
} else if (magnitude > 1){
color = "orange";
}
else {
color = "yellow";
}
return color
};
function createMap(earthquakes) {
// Define streetmap and darkmap layers
var streetmap = L.tileLayer("https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/{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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 14,
id: "mapbox.streets",
accessToken: API_KEY
});
// Define streetmap and darkmap layers
var outdoors = L.tileLayer("https://api.mapbox.com/styles/v1/mapbox/outdoors-v10/tiles/256/{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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 14,
id: "mapbox.streets",
accessToken: API_KEY
});
var darkmap = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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 © <a href=\"https://www.mapbox.com/\">Mapbox</a>",
maxZoom: 18,
id: "mapbox.dark",
accessToken: API_KEY
});
// Define a baseMaps object to hold our base layers
var baseMaps = {
"Outdoors": outdoors,
"Satellite": streetmap,
"Dark Map": darkmap
};
// Create overlay object to hold our overlay layer
var overlayMaps = {
Earthquakes: earthquakes
};
// Create our map, giving it the streetmap and earthquakes layers to display on load
var myMap = L.map("map", {
center: [
37.09, -95.71
],
zoom: 4,
layers: [streetmap, earthquakes]
});
// Create a layer control
// Pass in our baseMaps and overlayMaps
// Add the layer control to the map
L.control.layers(baseMaps, overlayMaps, {
collapsed: false
}).addTo(myMap);
// Create a legend to display information in the top left
var legend = L.control({position: 'topleft'});
legend.onAdd = function(map) {
var div = L.DomUtil.create('div','info legend'),
magnitudes = [0,1,2,3,4],
labels = [];
div.innerHTML += "<h4 style='margin:4px;text-align:center'>Earthquake Magnitude</h4>"
for (var i=0; i < magnitudes.length; i++){
div.innerHTML +=
'<hr><i style="background:' + dataColor(magnitudes[i] + 1) + '"></i> ' +
magnitudes[i] + (magnitudes[i+1]?'–' + magnitudes[i+1] +'<br>': '+');
}
return div;
};
legend.addTo(myMap);
}