UNPKG

leaflet.browser.print

Version:

A leaflet plugin which allows users to print the map directly from the browser

121 lines (96 loc) 2.97 kB
/** * * In this file are no leaflet.browser.print code samples * */ // control that shows state info on hover var info = L.control(); 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); // get color depending on population density value function getColor(d) { return d > 1000 ? '#800026' : d > 500 ? '#BD0026' : d > 200 ? '#E31A1C' : d > 100 ? '#FC4E2A' : d > 50 ? '#FD8D3C' : d > 20 ? '#FEB24C' : d > 10 ? '#FED976' : '#FFEDA0'; } function style(feature) { return { weight: 2, opacity: 1, color: 'white', dashArray: '3', fillOpacity: 0.7, fillColor: getColor(feature.properties.density) }; } function highlightFeature(e) { var layer = e.target; layer.setStyle({ weight: 5, color: '#666', dashArray: '', fillOpacity: 0.7 }); if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) { layer.bringToFront(); } info.update(layer.feature.properties); } var geojson; function resetHighlight(e) { geojson.resetStyle(e.target); info.update(); } function zoomToFeature(e) { map.fitBounds(e.target.getBounds()); } function onEachFeature(feature, layer) { layer.on({ mouseover: highlightFeature, mouseout: resetHighlight, click: zoomToFeature }); } geojson = L.geoJson(statesData, { style: style, onEachFeature: onEachFeature }).addTo(map); map.attributionControl.addAttribution('Population data &copy; <a href="http://census.gov/">US Census Bureau</a>'); /* Good way to create custom control */ L.LegendControl = L.Control.extend({ onAdd: function (map) { var div = L.DomUtil.create('div', 'info legend'); var grades = [0, 10, 20, 50, 100, 200, 500, 1000]; var labels = []; var from; var to; for (var i = 0; i < grades.length; i++) { from = grades[i]; to = grades[i + 1]; labels.push( '<div><i style="background:' + getColor(from + 1) + '"></i> ' + from + (to ? '&ndash;' + to : '+') + "</div>"); } div.innerHTML = labels.join(''); return div; } }); L.legendControl = function(options) { return new L.LegendControl(options); }; // Here we are creating control to show it on the map; L.legendControl({position: 'bottomright'}).addTo(map);