forked from Igor-Vladyka/leaflet.browser.print
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
247 lines (212 loc) · 8.39 KB
/
index.html
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html>
<head>
<title>Leaflet browser print plugin with custom print Layer and content</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"/>
<link rel="stylesheet" href="https://leaflet.github.io/Leaflet.draw/src/leaflet.draw.css"/>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/MarkerCluster.css"/>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/MarkerCluster.Default.css"/>
<style>
html, body { width: 100%; height: 100%; margin: 0px; padding: 0px;}
#map {
width: 80%; margin: auto; height: 70%;
}
</style>
</head>
<body>
<h1 class="title" leaflet-browser-print-content>Leaflet Browser print TITLE</h1>
<h3 class="sub-content" leaflet-browser-print-content>Leaflet browser print plugin for map printing with additional controls and content</h3>
<div id="map"></div>
<table leaflet-browser-print-pages>
<thead>
<tr>
<th>Option</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>title</td>
<td>String</td>
<td>'Print map'</td>
<td>Sets the text which appears as the tooltip of the print button</td>
</tr>
<tr>
<td>position</td>
<td><a href="http://leafletjs.com/reference-0.7.7.html#control-position" rel="nofollow">Leaflet control position</a></td>
<td>'topleft'</td>
<td>Position the print button</td>
</tr>
<tr>
<td>printModes</td>
<td>Array</td>
<td>["Portrait", "Landscape", "Auto", "Custom"]</td>
<td>Collection of page print actions</td>
</tr>
<tr>
<td>printModesNames</td>
<td>Object</td>
<td>{ Portrait: "Portrait", Landscape: "Landscape", Auto:"Auto", Custom:"Custom" }</td>
<td>Customize each print mode name</td>
</tr>
<tr>
<td>printLayer</td>
<td><a href="http://leafletjs.com/reference-0.7.7.html#tilelayer" rel="nofollow">Leaflet tile layer</a></td>
<td>null</td>
<td>A tiles layer to show instead of all current active tiles layers</td>
</tr>
<tr>
<td>closePopupsOnPrint</td>
<td>Boolean</td>
<td>true</td>
<td>Indicates if we need to force popup closing for printed map</td>
</tr>
<tr>
<td>contentSelector</td>
<td>String</td>
<td>"[leaflet-browser-print-content]"</td>
<td>Content selector for printed map, will select and dynamically inject content on printed maps. For full functionality please check "Printing additional content section"</td>
</tr></tbody></table>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.2/leaflet.draw.js"></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.markercluster.js"></script>
<script src="src/leaflet.browser.print.js"></script>
<script src="src/leaflet.browser.print.utils.js"></script>
<script src="src/leaflet.browser.print.sizes.js"></script>
<script>
//Layers
var cities = L.markerClusterGroup();
var arrayOfPoints = [[39.61, -105.02], [39.73, -104.8], [39.77, -105.23]];
L.marker(arrayOfPoints[0]).bindPopup('This is Littleton, CO.').addTo(cities),
L.marker(arrayOfPoints[1]).bindPopup('This is Aurora, CO.').addTo(cities),
L.marker(arrayOfPoints[2]).bindPopup('This is Golden, CO.').addTo(cities);
var p = L.polygon(arrayOfPoints, {color: 'yellow'}).bindTooltip("polygon").addTo(cities);
var mbAttr = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
mbUrl = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
var grayscale = L.tileLayer(mbUrl, {id: 'mapbox.light', attribution: mbAttr}),
streets = L.tileLayer(mbUrl, {id: 'mapbox.streets', attribution: mbAttr}),
originLayer = L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
})
wms = L.tileLayer.wms('https://ahocevar.com/geoserver/wms', { layers: 'topp:states', CQL_FILTER: 'PERSONS > 2000000', styles: '', format: 'image/png', transparent: true, });
var map = L.map('map', {
center: [39.73, -104.99],
zoom: 10,
layers: [grayscale, cities]
});
var baseLayers = {
"Grayscale": grayscale,
"Streets": streets,
"Origin": originLayer,
"WMS": wms
};
var overlays = {
"Cities": cities
};
L.control.layers(baseLayers, overlays).addTo(map);
var drawnItems = new L.FeatureGroup();
var drawControl = new L.Control.Draw({
position: 'bottomright',
edit: {
featureGroup: drawnItems,
remove: false,
edit: false
},
draw: {
circlemarker: false,
}
});
map.addControl(drawControl);
map.createPane("new-pane");
L.control.scale({position:'topleft'}).addTo(map);
map.on(L.Draw.Event.CREATED, function (e) {
var type = e.layerType,
layer = e.layer;
if (type === 'marker') {
layer.bindPopup('A popup!');
}
drawnItems.addLayer(layer);
map.addLayer(drawnItems);
});
var green = L.circle([39.73, -104.99], 5000, {
color: 'green',
fillColor: 'darkgreen',
fillOpacity: 0.8,
pane: "new-pane"
});
var l = L.layerGroup();
l.addTo(map)
l.addLayer(green);
var customActionToPrint = function(context) {
return function() {
window.alert("We are printing the MAP. Let's do Custom print here!");
context._printCustom();
}
}
L.control.browserPrint({
printLayer: L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
subdomains: 'abcd',
minZoom: 1,
maxZoom: 16,
ext: 'png'
}),
closePopupsOnPrint: false,
printModes: [
L.control.browserPrint.mode("Alert", "User specified print action", "A6", customActionToPrint, false),
L.control.browserPrint.mode.landscape(),
"PORTrait",
L.control.browserPrint.mode.auto("Auto", "B4"),
L.control.browserPrint.mode.custom("Séléctionnez la zone", "B5")
]
}).addTo(map);
L.Control.BrowserPrint.Utils.registerLayer(L.TileLayer.WMS, 'L.TileLayer.WMS', function(layer) {
console.info("Printing WMS layer.");
return L.tileLayer.wms(layer._url, layer.options);
});
L.popup({minWidth: 500}).setLatLng(L.latLng(39.73, -104.99)).setContent("Leaflet browser print plugin with custom print Layer and content").openOn(map);
</script>
<style>
.grid-print-container {
grid-template: auto 1fr auto / 1fr;
background-color: orange;
}
.grid-map-print {
grid-row: 2;
}
.grid-print-container > .title,
.grid-print-container > .sub-content {
color: white;
}
.title {
grid-row: 1;
justify-self: center;
text-align: center;
color: grey;
box-sizing: border-box;
}
.sub-content {
grid-row: 5;
padding-left: 10px;
text-align: center;
color: grey;
box-sizing: border-box;
}
</style>
<style>
[leaflet-browser-print-pages] {
display: none;
}
.pages-print-container [leaflet-browser-print-pages] {
display: block;
}
</style>
</body>
</html>