Default Data Layer: Earthquake data
by Wolf
HTML
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>
CSS
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 20,
lng: -160
},
zoom: 2
});
// Get the earthquake data (JSONP format)
// This feed is a copy from the USGS feed, you can find the originals here:
// http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
var script = document.createElement('script');
script.setAttribute(
'src',
'https://storage.googleapis.com/mapsdevsite/json/quakes.geo.json');
document.getElementsByTagName('head')[0].appendChild(script);
// Create the DIV to hold the control and call the CenterControl()
// constructor passing in this DIV.
var dataLayerControlDiv = document.createElement('div');
var dataLayerControl = new DataLayerControl(dataLayerControlDiv, map);
dataLayerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(dataLayerControlDiv);
}
// Defines the callback function referenced in the jsonp file.
function eqfeed_callback(data) {
map.data.addGeoJson(data);
}
/**
* The DataLayerControl adds a control to the map that toddles the data layer on and off.
* This constructor takes the control DIV as an argument.
* @constructor
*/
function DataLayerControl(controlDiv, map) {
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = '#fff';
controlUI.style.border = '2px solid #fff';
controlUI.style.borderRadius = '3px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
controlUI.style.cursor = 'pointer';
controlUI.style.marginBottom = '22px';
controlUI.style.textAlign = 'center';
controlUI.title = 'Click to toggle the markers on the map';
controlDiv.appendChild(controlUI);
// Set CSS for the control interior.
var controlText = document.createElement('div');
controlText.style.color = 'rgb(25,25,25)';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize =...