2011-06-10 19:26:16 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Leaflet debug page</title>
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../../dist/leaflet.css" />
|
2011-12-15 18:20:09 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
<link rel="stylesheet" href="../css/screen.css" />
|
2011-12-15 18:20:09 +08:00
|
|
|
|
2012-08-03 20:02:40 +08:00
|
|
|
<style>
|
2012-08-04 04:50:14 +08:00
|
|
|
#map {
|
|
|
|
width: 800px;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.info {
|
|
|
|
padding: 6px 8px;
|
2012-08-04 16:43:55 +08:00
|
|
|
font: 14px/16px Arial, Helvetica, sans-serif;
|
|
|
|
background: white;
|
|
|
|
background: rgba(255,255,255,0.8);
|
2012-08-03 20:02:40 +08:00
|
|
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
|
|
|
border-radius: 5px;
|
2012-08-04 04:50:14 +08:00
|
|
|
}
|
|
|
|
.info h4 {
|
|
|
|
margin: 0 0 5px;
|
|
|
|
color: #777;
|
|
|
|
}
|
|
|
|
|
|
|
|
.legend {
|
|
|
|
text-align: left;
|
|
|
|
line-height: 18px;
|
|
|
|
color: #555;
|
|
|
|
}
|
2012-08-04 16:43:55 +08:00
|
|
|
.legend i {
|
2012-08-04 04:50:14 +08:00
|
|
|
width: 18px;
|
|
|
|
height: 18px;
|
|
|
|
float: left;
|
|
|
|
margin-right: 8px;
|
|
|
|
opacity: 0.7;
|
2012-08-03 20:02:40 +08:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
2017-02-04 23:17:51 +08:00
|
|
|
<script src="../../build/deps.js"></script>
|
2011-06-10 19:26:16 +08:00
|
|
|
<script src="../leaflet-include.js"></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2012-08-04 04:50:14 +08:00
|
|
|
<div id="map"></div>
|
2011-06-10 19:26:16 +08:00
|
|
|
|
2017-02-04 23:17:51 +08:00
|
|
|
<script src="us-states.js"></script>
|
|
|
|
<script>
|
2011-06-10 19:26:16 +08:00
|
|
|
|
2012-08-04 04:50:14 +08:00
|
|
|
var map = L.map('map').setView([37.8, -96], 4);
|
2011-12-15 18:20:09 +08:00
|
|
|
|
2014-03-04 23:54:41 +08:00
|
|
|
var osm = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
|
2014-03-05 00:04:00 +08:00
|
|
|
attribution: '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
|
2012-08-03 20:40:16 +08:00
|
|
|
}).addTo(map);
|
|
|
|
|
|
|
|
|
2012-08-04 04:50:14 +08:00
|
|
|
// control that shows state info on hover
|
2012-08-07 16:56:48 +08:00
|
|
|
var info = L.control();
|
2012-08-03 19:49:45 +08:00
|
|
|
|
2012-08-07 16:56:48 +08:00
|
|
|
info.onAdd = function (map) {
|
|
|
|
this._div = L.DomUtil.create('div', 'info');
|
|
|
|
this.update();
|
|
|
|
return this._div;
|
|
|
|
};
|
|
|
|
|
|
|
|
info.update = function (props) {
|
|
|
|
this._div.innerHTML = '<h4>US Population Density</h4>' + (props ?
|
|
|
|
'<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
|
|
|
|
: 'Hover over a state');
|
|
|
|
};
|
|
|
|
|
|
|
|
info.addTo(map);
|
2012-08-03 19:49:45 +08:00
|
|
|
|
|
|
|
|
2012-08-04 04:50:14 +08:00
|
|
|
// get color depending on population density value
|
|
|
|
function getColor(d) {
|
|
|
|
return d > 1000 ? '#800026' :
|
2016-04-29 17:35:50 +08:00
|
|
|
d > 500 ? '#BD0026' :
|
|
|
|
d > 200 ? '#E31A1C' :
|
|
|
|
d > 100 ? '#FC4E2A' :
|
|
|
|
d > 50 ? '#FD8D3C' :
|
|
|
|
d > 20 ? '#FEB24C' :
|
|
|
|
d > 10 ? '#FED976' :
|
|
|
|
'#FFEDA0';
|
2012-08-04 04:50:14 +08:00
|
|
|
}
|
2012-08-03 19:49:45 +08:00
|
|
|
|
2012-08-04 04:50:14 +08:00
|
|
|
function style(feature) {
|
2012-08-04 16:43:55 +08:00
|
|
|
return {
|
|
|
|
weight: 2,
|
|
|
|
opacity: 1,
|
|
|
|
color: 'white',
|
|
|
|
dashArray: '3',
|
|
|
|
fillOpacity: 0.7,
|
2012-08-04 04:50:14 +08:00
|
|
|
fillColor: getColor(feature.properties.density)
|
2012-08-04 16:43:55 +08:00
|
|
|
};
|
2012-08-03 19:49:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function highlightFeature(e) {
|
2012-08-03 20:57:59 +08:00
|
|
|
var layer = e.target;
|
|
|
|
|
2012-08-08 17:18:05 +08:00
|
|
|
layer.setStyle({
|
2012-08-03 20:57:59 +08:00
|
|
|
weight: 5,
|
|
|
|
color: '#666',
|
2012-08-04 04:50:14 +08:00
|
|
|
dashArray: '',
|
|
|
|
fillOpacity: 0.7
|
2012-08-03 19:49:45 +08:00
|
|
|
});
|
2012-08-03 20:57:59 +08:00
|
|
|
|
2012-08-08 17:18:05 +08:00
|
|
|
if (!L.Browser.ie) {
|
|
|
|
layer.bringToFront();
|
|
|
|
}
|
|
|
|
|
2012-08-04 16:43:55 +08:00
|
|
|
info.update(layer.feature.properties);
|
2012-08-03 19:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-08-04 16:43:55 +08:00
|
|
|
var geojson;
|
2012-08-03 20:57:59 +08:00
|
|
|
|
2012-08-04 16:43:55 +08:00
|
|
|
function resetHighlight(e) {
|
|
|
|
geojson.resetStyle(e.target);
|
2012-08-04 04:50:14 +08:00
|
|
|
info.update();
|
2012-08-03 19:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-08-03 20:57:59 +08:00
|
|
|
function zoomToFeature(e) {
|
|
|
|
map.fitBounds(e.target.getBounds());
|
|
|
|
}
|
|
|
|
|
2012-08-03 19:49:45 +08:00
|
|
|
function onEachFeature(feature, layer) {
|
2012-08-04 16:43:55 +08:00
|
|
|
layer.on({
|
|
|
|
mouseover: highlightFeature,
|
|
|
|
mouseout: resetHighlight,
|
|
|
|
click: zoomToFeature
|
|
|
|
});
|
2012-08-03 19:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-08-04 16:43:55 +08:00
|
|
|
geojson = L.geoJson(statesData, {
|
2012-08-03 19:49:45 +08:00
|
|
|
style: style,
|
2016-11-03 16:32:56 +08:00
|
|
|
onEachFeature: onEachFeature,
|
|
|
|
attribution: 'Population data © <a href="http://census.gov/">US Census Bureau</a>'
|
2016-04-29 17:35:50 +08:00
|
|
|
});
|
2011-12-15 18:20:09 +08:00
|
|
|
|
2016-04-29 17:35:50 +08:00
|
|
|
geojson.addTo(map);
|
2012-08-04 04:50:14 +08:00
|
|
|
|
2012-08-04 16:43:55 +08:00
|
|
|
|
2012-08-07 16:56:48 +08:00
|
|
|
var legend = L.control({position: 'bottomright'});
|
2012-08-04 04:50:14 +08:00
|
|
|
|
2012-08-07 16:56:48 +08:00
|
|
|
legend.onAdd = function (map) {
|
2012-08-04 04:50:14 +08:00
|
|
|
|
2012-08-07 16:56:48 +08:00
|
|
|
var div = L.DomUtil.create('div', 'info legend'),
|
|
|
|
grades = [0, 10, 20, 50, 100, 200, 500, 1000],
|
|
|
|
labels = [],
|
|
|
|
from, to;
|
|
|
|
|
|
|
|
for (var i = 0; i < grades.length; i++) {
|
|
|
|
from = grades[i];
|
|
|
|
to = grades[i + 1];
|
|
|
|
|
|
|
|
labels.push(
|
|
|
|
'<i style="background:' + getColor(from + 1) + '"></i> ' +
|
|
|
|
from + (to ? '–' + to : '+'));
|
2012-08-04 04:50:14 +08:00
|
|
|
}
|
2012-08-04 16:43:55 +08:00
|
|
|
|
2012-08-07 16:56:48 +08:00
|
|
|
div.innerHTML = labels.join('<br>');
|
|
|
|
return div;
|
|
|
|
};
|
|
|
|
|
|
|
|
legend.addTo(map);
|
2012-08-04 04:50:14 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
</script>
|
|
|
|
</body>
|
2011-12-15 18:20:09 +08:00
|
|
|
</html>
|