mirror of
https://github.com/Leaflet/Leaflet.git
synced 2025-08-16 16:45:22 +00:00
improve geojson example
This commit is contained in:
@ -31,31 +31,35 @@
|
|||||||
|
|
||||||
var map = L.map('map').setView([37.8, -96.9], 4);
|
var map = L.map('map').setView([37.8, -96.9], 4);
|
||||||
|
|
||||||
L.control.scale().addTo(map);
|
|
||||||
|
|
||||||
var info = L.control();
|
|
||||||
|
|
||||||
info.onAdd = function (map) {
|
|
||||||
var div = this._div = L.DomUtil.create('div', 'state-info');
|
|
||||||
info.setFeature(null);
|
|
||||||
return div;
|
|
||||||
}
|
|
||||||
|
|
||||||
info.setFeature = function (feature) {
|
|
||||||
this._div.innerHTML = feature ?
|
|
||||||
'<b>' + feature.properties.name + '</b>' +
|
|
||||||
'<br>' + feature.properties.density + ' /mi<sup>2</sup>'
|
|
||||||
: 'Hover over a state';
|
|
||||||
}
|
|
||||||
|
|
||||||
info.addTo(map);
|
|
||||||
|
|
||||||
var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
|
var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
|
||||||
attribution: 'Map data © 2011 OpenStreetMap contributors, Imagery © 2011 CloudMade',
|
attribution: 'Map data © 2011 OpenStreetMap contributors, Imagery © 2011 CloudMade',
|
||||||
key: 'BC9A493B41014CAABB98F0471D759707',
|
key: 'BC9A493B41014CAABB98F0471D759707',
|
||||||
styleId: 22677
|
styleId: 22677
|
||||||
}).addTo(map);
|
}).addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
L.control.scale().addTo(map);
|
||||||
|
|
||||||
|
|
||||||
|
var info = L.Util.extend(L.control(), {
|
||||||
|
|
||||||
|
onAdd: function (map) {
|
||||||
|
var div = this._div = L.DomUtil.create('div', 'state-info');
|
||||||
|
info.setFeature(null);
|
||||||
|
return div;
|
||||||
|
},
|
||||||
|
|
||||||
|
setFeature: function (feature) {
|
||||||
|
this._div.innerHTML = feature ?
|
||||||
|
'<b>' + feature.properties.name + '</b>' +
|
||||||
|
'<br>' + feature.properties.density + ' /mi<sup>2</sup>'
|
||||||
|
: 'Hover over a state';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
info.addTo(map);
|
||||||
|
|
||||||
|
|
||||||
var defaultStyle = {
|
var defaultStyle = {
|
||||||
weight: 2,
|
weight: 2,
|
||||||
opacity: 1,
|
opacity: 1,
|
||||||
@ -65,14 +69,11 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
function style(feature) {
|
function style(feature) {
|
||||||
var value = (Math.log(Math.min(feature.properties.density, 1000)) / Math.LN10) / 3,
|
var value = Math.min((Math.log(feature.properties.density) / Math.LN10) / 3, 1),
|
||||||
fromHue = 180,
|
hue = Math.round(160 * (1 - value)),
|
||||||
toHue = 0,
|
style = {fillColor: 'hsl(' + hue + ',90%,42%)'};
|
||||||
hue = Math.round(fromHue + value * (toHue - fromHue));
|
|
||||||
|
|
||||||
return L.Util.extend({}, defaultStyle, {
|
return L.Util.extend({}, defaultStyle, style);
|
||||||
fillColor: 'hsl(' + hue + ',90%,42%)'
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function highlightFeature(e) {
|
function highlightFeature(e) {
|
||||||
@ -91,11 +92,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onEachFeature(feature, layer) {
|
function onEachFeature(feature, layer) {
|
||||||
//layer.bindPopup('<b>' + feature.properties.name + '</b>');
|
|
||||||
|
|
||||||
layer.feature = feature;
|
layer.feature = feature;
|
||||||
|
layer.on('mouseover', highlightFeature);
|
||||||
layer.on('mouseover click', highlightFeature);
|
|
||||||
layer.on('mouseout', resetHighlight);
|
layer.on('mouseout', resetHighlight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user