Google Maps GeoJSON: Earthquake circles
by kmg747
HTML
<div id="map"></div>
<div id="info-box">hello</div><button onclick="removeAll()">Remove All</button>
<!-- 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: 90%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
var map;
var infowindow;
var features;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: {lat: -9, lng: 147},
mapTypeId: 'terrain'
});
// Create a <script> tag and set the USGS URL as the source.
var script = document.createElement('script');
// This example uses a local copy of the GeoJSON stored at
// http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp
script.src = 'https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js';
document.getElementsByTagName('head')[0].appendChild(script);
map.data.setStyle(function(feature) {
var magnitude = feature.getProperty('mag');
return {
icon: getCircle(magnitude)
};
});
infowindow = new google.maps.InfoWindow();
map.data.addListener('mouseover', function(event) {
var myHTML = event.feature.getProperty('mag');
infowindow.setContent("<div style='width:50px; text-align: center;'>"+myHTML+"</div>");
infowindow.setPosition(event.feature.getGeometry().get());
infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
infowindow.open(map);
document.getElementById('info-box').textContent =
event.feature.getProperty('place');
});
}
function removeAll() {
console.log('remove all');
infowindow.close();
for (var i = 0; i < features.length; i++)
map.data.remove(features[i]);
}
function getCircle(magnitude) {
return {
path: google.maps.SymbolPath.CIRCLE,
fillColor: 'red',
fillOpacity: .2,
scale: Math.pow(2, magnitude) / 2,
strokeColor: 'white',
strokeWeight: .5
};
}
function eqfeed_callback(results) {
features = map.data.addGeoJson(results);
}