-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
145 lines (111 loc) · 4.22 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
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
function createMap(earthquakes) {
// Create the tile layer that will be the background of our map
var lightmap = L.tileLayer("https://api.mapbox.com/styles/v1/mapbox/light-v9/tiles/256/{z}/{x}/{y}?access_token={accessToken}", {
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>",
maxZoom: 18,
id: "mapbox.light",
accessToken: API_KEY
});
// Create a baseMaps object to hold the lightmap layer
var baseMaps = {
"Light Map": lightmap
};
// Create an overlayMaps object to hold the earthquakes layer
var overlayMaps = {
"Earthquakes": earthquakes
};
document.getElementById('map-id').innerHTML = "<div id='map' style='width: 100%; height: 100%;'></div>";
var map = L.map("map", {
center: [34.0522,-118.2437],
zoom: 4,
layers:
[lightmap, earthquakes]
});
// Create a layer control, pass in the baseMaps and overlayMaps. Add the layer control to the map
L.control.layers(baseMaps, overlayMaps, {
collapsed: false
}).addTo(map);
// Create a legend to display information about our map
var legend = L.control({
position: "bottomright"
});
// When the layer control is added, insert a div with the class of "legend"
legend.onAdd = function() {
var div = L.DomUtil.create('div', 'info legend'),
magnitudes = [0, 1, 2, 3, 4, 5];
labels = []
for (var i = 0; i < magnitudes.length; i++) {
div.innerHTML +=
'<i style="background:' + markerColor(magnitudes[i] + 1) + '"></i> ' +
magnitudes[i] + (magnitudes[i + 1] ? '–' + magnitudes[i + 1] + '<br>' : '+');
}
return div;
};
// Add the legend to the map
legend.addTo(map);
}
function markerSize(mag) {
return mag * 30000;
}
function markerColor(mag) {
var cols=["#c1ebb5", "#bfc176","#c29348","#c05f37","#b11f3f", "#E6E67F"];
if (mag <= 1) {
return cols[0];
} else if (mag <= 2) {
return cols[1];
} else if (mag <= 3) {
return cols[2];
} else if (mag <= 4) {
return cols[3];
} else if (mag <= 5) {
return cols[4];
} else {
return cols[5]
};
}
function createMarkers(response) {
// Pull the quake features off of response
var result = response.features;
console.log("this is results" ,result)
// Initialize an array to hold quake markers
var quakeMarkers = [];
// Loop through the stations array
quakeRes=result.length
for (var index = 0; index < quakeRes; index++) {
var data = result[index].properties;
coords=result[index].geometry.coordinates
//console.log("lat",coords[0])
//console.log("lng",coords[1])
// For each quake, create a circle marker and bind a popup with the location name + magnitude
var circleMarker= L.circle([coords[1], coords[0]],
{radius: markerSize(data.mag),
fillColor: markerColor(data.mag),
fillOpacity: 1,
stroke: false,
}).bindPopup("<h3><h3>Reported Earthquake " + "<h3><h3>Magnitude: " + data.mag + "<h3><h3>Location: "+ data.place+"<h3>");
// Add the marker to quakeMarkers array
quakeMarkers.push(circleMarker);
}
// Create a layer group made from the quakeMarkers array, pass it into the createMap function
createMap(L.layerGroup(quakeMarkers));
}
// map function for reported hourly/ weekly/ monthly earthquakes
function runMap(){
//set default json source URL
var source="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson"
//get the dropdown value
var e = document.getElementById("period");
var period = e.options[e.selectedIndex].value;
//change json response based on dropdown value
if (period=="monthly"){
source="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson";
}else if (period=="weekly") {
source="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson";
}else if (period="hourly") {
source="https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson";
}
// Perform an API call to the USGS information endpoint
d3.json(source, createMarkers);
}
//iNitialize the runmap
runMap()