Created
May 25, 2016 17:46
-
-
Save peterqliu/0345753637296b626b0a1f75f6050ef8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title></title> | |
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.css' rel='stylesheet' /> | |
<style> | |
body { margin:0; padding:0; background:black;} | |
#map { position:absolute; top:0; bottom:0; right:0; left:0px; } | |
.mapboxgl-popup-content {padding:10px; background:#333; color:#ccc;} | |
.mapboxgl-popup-anchor-bottom .mapboxgl-popup-tip {border-top-color:#333;} | |
.mapboxgl-popup-anchor-top .mapboxgl-popup-tip {border-bottom-color:#333;} | |
.mapboxgl-popup-anchor-left .mapboxgl-popup-tip {border-right-color:#333;} | |
.mapboxgl-popup-anchor-right .mapboxgl-popup-tip {border-left-color:#333;} | |
.mapboxgl-popup {pointer-events:none;} | |
h2 {margin:10px 10px 10px 0px;} | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoicGV0ZXJxbGl1IiwiYSI6ImpvZmV0UEEifQ._D4bRmVcGfJvo1wjuOpA1g'; | |
var ne = new mapboxgl.LngLat(-30, 90); | |
var sw = new mapboxgl.LngLat(360, -90); | |
var llb = new mapboxgl.LngLatBounds(sw, ne); | |
var map = new mapboxgl.Map({ | |
container: 'map', // container id | |
style: 'mapbox://styles/peterqliu/cioln49d3005lavm36kvbe8ul', //stylesheet location | |
center: [180,30], // starting position | |
zoom: 1, // starting zoom | |
maxBounds: llb | |
}); | |
mapboxgl.util.getJSON('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson', function(err,json){ | |
console.log(json) | |
var timeObj = { | |
"property": 'time', | |
"stops": [ | |
[Date.now()-259200000, '#fff'], | |
[Date.now(), 'white'] | |
] | |
} | |
map.on('load', function(err, resp){ | |
map.addSource('quakes', { | |
type: 'geojson', | |
data: { | |
"type": "FeatureCollection", | |
"features": [] | |
} | |
}) | |
.addLayer({ | |
"id": "quake", | |
"type": "circle", | |
"source": "quakes", | |
"layout": { | |
}, | |
"paint": { | |
"circle-color": "hsl(204, 33%, 32%)", | |
"circle-opacity": { | |
"base": 1.8, | |
'property': 'mag', | |
"stops": [ | |
[{zoom: 0, value: 2}, 0.01], | |
[{zoom: 0, value: 6}, 0.3], | |
[{zoom: 11, value: 2}, 0.1], | |
[{zoom: 11, value: 6}, 0.8], | |
[{zoom: 20, value: 2}, 0.8], | |
[{zoom: 20, value: 6}, 1] | |
] | |
}, | |
'circle-radius': { | |
'property': 'mag', | |
"base": 1.75, | |
'stops': [ | |
[{zoom: 0, value: 2}, 1], | |
[{zoom: 0, value: 6}, 36], | |
[{zoom: 11, value: 2}, 64], | |
[{zoom: 11, value: 6}, 400], | |
[{zoom: 20, value: 2}, 100], | |
[{zoom: 20, value: 6}, 2800] | |
] | |
}, | |
'circle-blur': 1 | |
} | |
}) | |
.addLayer({ | |
"id": "smalldot", | |
"type": "circle", | |
"source": "quakes", | |
"layout": { | |
}, | |
"paint": { | |
"circle-color": 'white', | |
'circle-radius': { | |
"base": 1.15, | |
"stops": [ | |
[0, 0.5], | |
[20, 10] | |
] | |
} | |
} | |
}); | |
map.getSource('quakes').setData(json); | |
var popup = new mapboxgl.Popup({ | |
closeButton: false, | |
closeOnClick: false | |
}) | |
map.on('mousemove', function(e) { | |
var feature = map.queryRenderedFeatures([[e.point.x-5,e.point.y-5],[e.point.x+5,e.point.y+5]], {layers:['smalldot']})[0]; | |
popup.remove(); | |
if (feature) map.getCanvas().style.cursor = 'pointer' ; | |
var people = feature.properties.felt>0 ? feature.properties.felt+' people' : 'nobody' | |
var coords = (feature.geometry.coordinates) | |
coords[0] = coords[0]<0 ? coords[0]+360 : coords[0] | |
popup | |
.setLngLat(coords) | |
.setHTML('<h2 style="float:left; margin-right:10px">'+feature.properties.mag.toFixed(1)+'</h2>'+feature.properties.place+'<div style="white-space:nowrap">'+((Date.now()-feature.properties.time)/86400000).toFixed(0)+' days ago ('+people+' felt it)</div>') | |
.addTo(map); | |
}); | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment