-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
114 lines (94 loc) · 3.66 KB
/
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
// Perform API call to USGS API to get earthquake data
d3.json("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson", function(earthquakeData) {
createFeatures(earthquakeData.features);
});
// Function to scale the Magnitude
function markerSize(magnitude) {
return magnitude * 30000;
};
// Function to assign color depends on the Magnitude
function getColor(m) {
var colors = ['lightgreen','yellowgreen','gold','orange','lightsalmon','tomato'];
return m > 5? colors[5]:
m > 4? colors[4]:
m > 3? colors[3]:
m > 2? colors[2]:
m > 1? colors[1]:
colors[0];
};
function createFeatures(earthquakeData) {
var earthquakes = L.geoJSON(earthquakeData,{
// Give each feature a popup describing with information pertinent to it
onEachFeature: function(feature, layer){
layer.bindPopup("<h3 > Magnitude: "+ feature.properties.mag +
"</h3><h3>Location: " + feature.properties.place +
"</h3><hr><h3>" + new Date(feature.properties.time) + "</h3>" );
},
pointToLayer: function(feature, latlng){
return new L.circle(latlng,
{ radius: markerSize(feature.properties.mag),
fillColor: getColor(feature.properties.mag),
fillOpacity: .8,
color: 'grey',
weight: .5
})
}
});
createMap(earthquakes);
};
function createMap(earthquakes) {
// Define lightmap, outdoorsmap, and satelliemap layers
let mapboxUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}';
let accessToken = 'pk.eyJ1IjoiY2FwMDE1NzAwIiwiYSI6ImNqZng1ZjBhbjQxMWozM21kZzkzNW1kdjAifQ.VdaKJu8FPaDob9yWS4kTSw';
let lightmap = L.tileLayer(mapboxUrl, {id: 'mapbox.light', maxZoom: 20, accessToken: accessToken});
let outdoorsmap = L.tileLayer(mapboxUrl, {id: 'mapbox.run-bike-hike', maxZoom: 20, accessToken: accessToken});
let satellitemap = L.tileLayer(mapboxUrl, {id: 'mapbox.streets-satellite', maxZoom: 20, accessToken: accessToken});
var tectonicPlates = new L.LayerGroup();
d3.json("https://raw.githubusercontent.com/fraxen/tectonicplates/master/GeoJSON/PB2002_boundaries.json", function (plateData) {
L.geoJSON(plateData,
{
color: 'orange',
weight: 2
})
.addTo(tectonicPlates);
});
// Define a baseMaps object to hold our base layers
var baseMaps = {
"Grayscle": lightmap,
"Outdoors": outdoorsmap,
"Satellite Map" : satellitemap
};
// Create overlay object to hold our overlay layer
var overlayMaps = {
"Earthquakes": earthquakes,
"Tectonic Plates": tectonicPlates
};
// Create our map, giving it the lightmap and earthquakes layers to display on load
var myMap = L.map("map-id", {
center: [39.8283, -98.5795],
zoom: 3,
layers: [lightmap, 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 bottom right
var legend = L.control({position: 'bottomright'});
legend.onAdd = function(map) {
var div = L.DomUtil.create('div','info legend'),
magnitudes = [0,1,2,3,4,5],
labels = [];
div.innerHTML += "<h4 style='margin:4px'>Magnitude</h4>"
// loop through our density intervals and generate a label for each interval
for (var i=0; i < magnitudes.length; i++){
div.innerHTML +=
'<i style="background:' + getColor(magnitudes[i] + 1) + '"></i> ' +
magnitudes[i] + (magnitudes[i+1]?'–' + magnitudes[i+1] +'<br>': '+');
}
return div;
};
legend.addTo(myMap);
}