Creating Leaflet image overlays based on bounding box and properties of GeoJSON data
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.js"></script>
<script src="//lnaweisu.github.io/leaflet-polygon-fillPattern/leaflet-polygon.fillPattern.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/leaflet.css">
<div id="map">
</div>
CSS
#map {
height: 600px;
width: 100%;
}
JavaScript
////////////////////////////////////////////////////////////////////////////////////////////
//setting up the map//
////////////////////////////////////////////////////////////////////////////////////////////
var map = L.map('map').setView([34, -118], 10);
ATTR = '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' +
'© <a href="http://cartodb.com/attributions">CartoDB</a>';
CDB_URL = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';
L.tileLayer(CDB_URL, {
attribution: ATTR
}).addTo(map);
/////////////////////////////////////////////////////////////////////////////////////////////
//displaying data//
/////////////////////////////////////////////////////////////////////////////////////////////
var mapInfo = getJson();
function getImage(d) {
return d === 'Freezing Rain' ? "http://freeiconbox.com/icon/256/17792.png" :
d === 'Thunderstorm' ? "http://freeiconbox.com/icon/256/17785.png" :
d === 'Heavy Rain' ? "http://freeiconbox.com/icon/256/27821.png" :
d === 'Gale\/Storm' ? "http://freeiconbox.com/icon/256/16474.png" :
"http://freeiconbox.com/icon/256/16606.png";
}
//create image overlays based on field_hazards property
function onEachBox(feature, layer) {
var imageBounds = layer.getBounds();
var imageUrl = getImage(feature.properties.field_hazards);
var imageLayer = L.imageOverlay(imageUrl, imageBounds).addTo(map).bringToBack();
layer.bindPopup(feature.properties.field_hazards);
}
//transparent box for GeoJSON overlay
var boxOptions = {
fillOpacity: 0,
opacity: 0,
onEachFeature: onEachBox
};
//create the image interaction box
var imageBox = L.geoJson(mapInfo, boxOptions).addTo(map);
//zoom in to fit GeoJSON...