ol-ext/examples/map.style.photo.html
2016-05-29 20:35:42 +02:00

164 lines
5.7 KiB
HTML

<!DOCTYPE html>
<!----------------------------------------------------------
Copyright (c) 2015 Jean-Marc VIGLINO,
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
------------------------------------------------------------>
<html>
<head>
<title>OL3-ext: Image Photo Style</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="ol.style.Chart is an image style to draw statistical graphics (bar or pie charts) on a map." />
<meta name="keywords" content="ol3, style, vector, statistic, chart, pie" />
<!-- jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- OL3 -->
<link rel="stylesheet" href="http://openlayers.org/en/master/css/ol.css" />
<script type="text/javascript" src="http://openlayers.org/en/master/build/ol.js"></script>
<!-- Photo style -->
<script type="text/javascript" src="../style/imagecanvasstyle.js"></script>
<script type="text/javascript" src="../style/photostyle.js"></script>
<script type="text/javascript" src="../utils/ol.ordering.js"></script>
<link rel="stylesheet" href="style.css" />
<style>
.options { max-width:400px; }
.options p { margin:0; font-size:0.9em; }
.options p.copy
{ font-size: 0.8em;
color: navy;
}
</style>
</head>
<body >
<a href="https://github.com/Viglino/ol3-ext"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
<a href="../index.html">
<h1>OL3-ext: Image Photo Style</h1>
</a>
<p class="info">
The <i>ol.style.Photo</i> is a style to show photos or images on a map.
<br/>
The photo are drawn in a box, a circle and can be anchored.
</p>
<!-- DIV pour la carte -->
<div id="map" style="width:600px; height:400px;"></div>
<div class="options">
<h2>Options:</h2>
<ul>
<li>
kind:
<select id="kind" onchange="vector.changed();">
<option value="default">default</option>
<option value="square">square</option>
<option value="circle">circle</option>
<option value="anchored">anchored</option>
<option value="folio">folio</option>
</select>
</li>
<li>
border:
<input id="border" type="number" onchange="vector.changed();" value="3" style="width:4em" />
</li>
<li>
<input id="shadow" type="checkbox" checked="checked" onchange="vector.changed();" />
<label for="shadow"> shadow</label>
</li>
<li>
<input id="crop" type="checkbox" checked="checked" onchange="vector.changed();" />
<label for="crop"> crop image within square</label>
</li>
</ul>
</div>
<div id ="select" class="options" >Select an image.</div>
<div id="map3d" style="position:relative; clear:both; width:600px; height:400px;"></div>
<script type="text/javascript">
// Layers
var layer = new ol.layer.Tile({ source: new ol.source.Stamen({ layer: 'watercolor' }) });
// The map
var map = new ol.Map
({ target: 'map',
view: new ol.View
({ zoom: 6,
center: [166326, 5992663]
}),
layers: [layer]
});
// Array to cache image style
var styleCache = {};
// Vector style
function getFeatureStyle (feature, resolution, sel)
{ var k = $('#kind').val()+"_"+$("#border").val()+"_"+feature.get("img").match(/[^\\/]+$/)[0]+($("#shadow").prop('checked')?"_1":"_0")+($("#crop").prop('checked')?"_1":"_0")+(sel?"_1":"");
var style = styleCache[k];
if (!style)
{ styleCache[k] = style = new ol.style.Style
({ image: new ol.style.Photo (
{ src: feature.get("img"),
radius: 20,
crop: $("#crop").prop('checked'),
kind: $('#kind').val(),
shadow: $("#shadow").prop('checked')?5:0,
onload: function() { vector.changed(); },
stroke: new ol.style.Stroke(
{ width: Number($("#border").val()) + (sel ? 3 : 0),
color: sel ? 'red' : '#fff'
})
})
});
}
return [style];
}
// GeoJSON layer
var vectorSource = new ol.source.Vector(
{ url: 'data/fond_guerre.geojson',
projection: 'EPSG:3857',
format: new ol.format.GeoJSON(),
attributions: [new ol.Attribution({ html: "&copy; <a href='https://www.data.gouv.fr/fr/datasets/fonds-de-la-guerre-14-18-extrait-de-la-base-memoire/'>data.gouv.fr</a>" })],
logo:"https://www.data.gouv.fr/s/avatars/37/e56718abd4465985ddde68b33be1ef.jpg"
});
var vector = new ol.layer.Vector(
{ name: '1914-18',
preview: "http://www.culture.gouv.fr/Wave/image/memoire/2445/sap40_z0004141_v.jpg",
source: vectorSource,
// y ordering
renderOrder: ol.ordering.yOrdering(),
style: getFeatureStyle
});
map.addLayer(vector);
// Control Select
var select = new ol.interaction.Select(
{ style: function (feature, resolution) { return getFeatureStyle(feature, resolution, true); }
})
map.addInteraction(select);
// onselect
select.getFeatures().on(['add','remove'], function(e)
{ if (e.type=="add")
{ var info = $("#select").html("<p>Selection:</p>");
var el = e.element;
$("<img>").attr('src',el.get("img")).appendTo(info);
$("<p>").text(el.get("text")).appendTo(info);
$("<p>").text(el.get("commune")+" ("+el.get("region")+" - "+el.get("date").replace(".","/")+")").appendTo(info);
$("<p>").addClass('copy').html("&copy; "+el.get("copy")+" - "+el.get("author")).appendTo(info);
}
else $("#select").html("<p>Select an image.</p>");
});
</script>
</body>
</html>