1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| <!DOCTYPE HTML>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leaflet CRS.Simple</title>
<meta name="Author" content="NoSmoking">
<meta name="DVP-discussion" content="d2017643">
<link href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" rel="stylesheet">
<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"></script>
<style>
#div-map {
width: 800px;
height: 600px;
margin: 1em auto;
border: 1px solid #069;
background: #FFF;
}
</style>
</head>
<body>
<h1>Leaflet: L.CRS.Simple</h1>
<div id="div-map"></div>
<script>
// taille canvas souhaité
const canvasSize = {
width: 10,
height: 8
}
// création limites
const mapBounds = [ [0,0], [canvasSize.height, canvasSize.width]];
const oMap = L.map("div-map", {
zoomSnap: 0.1, // permet un zoom plus précis
crs: L.CRS.Simple
});
oMap.fitBounds(mapBounds);
////////////////////////////
function setRectangle(map, rect, option) {
// rect => [[bottom, left], [top, right]]
const pSW = rect[0].slice(0);
const pNE = rect[1].slice(0);
pSW[0] = canvasSize.height - pSW[0];
pNE[0] = canvasSize.height - pNE[0];
const layer = L.rectangle([pSW, pNE], option || {}).addTo(map);
return layer;
}
const data = [{
bound: mapBounds,
option: { color: "#89A", weight: 0}
},
{
bound: [[0, 0], [1, 1]],
option: { color: "#000", weight: 1}
},
{
bound: [[2, 2], [3, 3]],
option: { color: "#F00", weight: 1}
},
{
bound: [[2, 1], [3, 2]],
option: { color: "#FFF", weight: 1}
},
{
bound: [[3, 2], [4, 3]],
option: { color: "#FF0", weight: 1}
},
{
bound: [[5, 2], [4, 3]],
option: { color: "#00F", weight: 1}
},
{
bound: [[4, 1], [3, 2]],
option: { color: "#0FF", weight: 1}
}
];
/* coordonnées cartésiennes
data.forEach(function (el) {
L.rectangle(el.bound, el.option).addTo(oMap);
});
/*--*/
data.forEach(function (el) {
setRectangle(oMap, el.bound, el.option);
});
</script>
</body>
</html> |
Partager