forked from maptimeBoston/leaflet-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·255 lines (233 loc) · 9.93 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
248
249
250
251
252
253
254
255
<!DOCTYPE html><html><head><title></title><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /><style type='text/css'>
body {
font-family:'Helvetica';
letter-spacing:-5px;
background:#000;
background-size:100%;
color:#fff;
margin:0;
padding:0;
font-weight:bold;
}
h1, h2, h3, p {
margin:0;
}
em, a {
font-style:normal;
color:#8dbd0c;
}
a {
background:#34d0e7;
color:#000;
text-decoration:none;
}
img {
width:100%;
}
pre {
margin:0;
padding:0.2em;
background:#fff;
color:#000;
font-weight:normal;
}
pre em {
color:#000;
background:yellow;
}
div {
cursor:pointer;
cursor:hand;
position:absolute;
top:0px;
left:0px;
padding:75px;
line-height:97%;
}
div.center {
text-align:center;
}
div.imageText {
text-shadow:0px 0px 5px rgba(0,0,0,0.2);
}
notes {
display:none;
}
/* normally not good, but ok in context of full screen directional navigation */
:focus {
outline: 0;
}
</style><script type='text/javascript'>
window.onload = function() {
var s = document.getElementsByTagName('div'), ti, i;
for (i = 0; i < s.length; i++) s[i].setAttribute('tabindex', 0);
if (!s.length) return;
var big = window.big = { current: 0, forward: fwd, reverse: rev, go: go, length: s.length };
function resize() {
var w = window.innerWidth, h = window.innerHeight, e = s[big.current];
e.style.fontSize = h + 'px';
function pass(cinch, start) {
for (var i = start; i > 0 && (e.offsetWidth > w || e.offsetHeight > h); i -= cinch) {
e.style.fontSize = i + 'px';
}
return i + cinch;
}
pass(2, pass(5, pass(10, h - 2)));
e.style.marginTop = (h - e.offsetHeight) / 2 + 'px';
}
function go(n) {
big.current = n;
var e = s[n], t = parseInt(e.getAttribute('data-timeToNext') || 0, 10),
notes = e.getElementsByTagName('notes');
document.body.className = e.getAttribute('data-bodyclass') || '';
for (i = 0; i < s.length; i++) s[i].style.display = 'none';
e.style.display = 'inline';
e.focus();
for (i = 0; typeof console === 'object' && i < notes.length; i++) console.log('%c%s: %s', 'padding:5px;font-family:serif;font-size:18px;line-height:150%;', n, notes[i].innerHTML.trim());
if (e.firstChild && e.firstChild.nodeName === 'IMG') {
document.body.style.backgroundImage = 'url("' + e.firstChild.src + '")';
e.firstChild.style.display = 'none';
if ('classList' in e) e.classList.add('imageText');
} else {
document.body.style.backgroundImage = '';
document.body.style.backgroundColor = e.style.backgroundColor;
}
if (ti !== undefined) window.clearInterval(ti);
if (t > 0) ti = window.setTimeout(fwd, t * 1000);
resize();
if (window.location.hash !== n) window.location.hash = n;
document.title = e.textContent || e.innerText;
}
document.onclick = function() { go(++big.current % s.length); };
function fwd() { go(Math.min(s.length - 1, ++big.current)); }
function rev() { go(Math.max(0, --big.current)); }
document.onkeydown = function(e) {
if (e.which === 39 || e.which === 34 || e.which === 40) fwd();
if (e.which === 37 || e.which === 33 || e.which === 38) rev();
};
document.ontouchstart = function(e) {
var x0 = e.changedTouches[0].pageX;
document.ontouchend = function(e2) {
var x1 = e2.changedTouches[0].pageX;
if (x1 - x0 < 0) fwd();
if (x1 - x0 > 0) rev();
};
};
function parse_hash() {
return Math.max(Math.min(s.length - 1,
parseInt(window.location.hash.substring(1), 10)), 0);
}
if (window.location.hash) big.current = parse_hash() || big.current;
window.onhashchange = function() {
i = parse_hash();
if (i !== big.current) go(i);
};
window.onresize = resize;
go(big.current);
};
</script></head><body>
<div>Super fast crash-tour of... </div>
<div><img src="ratmap/bostonleaflet.png"><em>Leaflet!</em></div>
<div>(Taking a page outta maptime boston's <a href="http://maptimeboston.github.io/leaflet-intro/">book</a>)</div>
<div> quick review </div>
<div><img src="images/anatomy-of-a-web-map.png"> <em> Anatomy of Web Map </em></div>
<div><img src="images/anatomy-of-a-web-map.png"></div>
<div>{.js}</div>
<div><br>* grabs the tiles<br> * adds content layer<br> * handles interaction</div>
<div><em>leaflet.js</em></div>
<div>What is <em>leaflet.js</em> for?</div>
<div>"slippy" maps with tiled base layers</div>
<div><img src="images/slippy-map.gif"></div>
<div>What does <em>leaflet.js</em> do?</div>
<div>handles basic tasks like converting data to map layers</div>
<div>panning and zooming and mouse interactions</div>
<div>What Leaflet does <em>not</em> do?</div>
<div>Provide any data. It's up to you to provide map data, including a basemap</div>
<div>Why do you like it <em>so much</em>?</div>
<div>Extremely lightweight — around 34 KB of gzipped JS code</div>
<div>No external dependencies</div>
<div>works well across most types of devices</div>
<div>open source</div>
<div>easy to extend with plugins</div>
<div data-timeToNext="1">Geoprocessing</div>
<div data-timeToNext="1">Routing</div>
<div data-timeToNext="1">Geocoding</div>
<div data-timeToNext="1">Dynamic data loading</div>
<div data-timeToNext="1">Time. Elevation. Search. </div>
<div>So here's how you <em>use</em> it!</div>
<div>The simplest map needs:</div>
<div>leaflet <em>CSS</em> styles</div>
<div>leaflet.js <em>library</em></div>
<div><em>div</em> element to hold the map </div>
<div><em>height</em> style specified for the map div</div>
<div>short <em>script</em> to create the map in that div</div>
<div><img src="images/leaflet_code.png"></div>
<div><img src="ratmap/simplestmap.png"></div>
<div>Remember tiles?</div>
<div><img src="images/tiles.gif"></div>
<div>http://<em>tile.openstreetmap.org</em>/4/2/6.png <br>-- tile server.</div>
<div>http://tile.openstreetmap.org/<em>4</em>/2/6.png <br>-- zoom level.</div>
<div>http://tile.openstreetmap.org/4/<em>2/6</em>.png <br>-- place in the grid where the tile lives.</div>
<div>Leaflet uses p-much any map <em>tile</em> <a href="http://leaflet-extras.github.io/leaflet-providers/preview/">provider</a></div>
<div> <a href="http://wiki.openstreetmap.org/wiki/Tiles">OpenStreetMap</a>, <a href="http://cloudmade.com/products/map-tiles">CloudMade</a>, <a href="http://maps.stamen.com">Stamen</a>, <a href="https://www.mapbox.com/tour/#maps">MapBox</a>, <a href="http://developer.mapquest.com/web/products/open/map">MapQuest</a></div>
<div>Sometimes maps use <em>multiple</em> tile layers at once!</div>
<div>Just pile on additional layers the same way as the first one.</div>
<div><img src="ratmap/multi-tiles.png"></div>
<div><img src="images/giphy.gif"></div>
<div>MOAR LAYERZ!</div>
<div>features</div>
<div><img src="images/basemap-datalayers-01.png"></div>
<div><img src="images/basemap-datalayers-03.png"></div>
<div><img src="images/basemap-datalayers-05.png"></div>
<div><img src="ratmap/importdata.png"></div>
<div>use jQuery's <em>getJSON()</em> method to load the data file</div>
<div>pass:<br> a) path to geodata file<br> b) a callback</div>
<div><img src="ratmap/importdata.png"></div>
<div>we use <em>L.geoJson()</em> to create a vector layer from GeoJSON</div>
<div>again using <em>addTo()</em> to put the layer on the map</div>
<div><img src="ratmap/importdata.png"></div>
<div>Did you know? leaflet supports default and custom markers</div>
<div>Why would we want custom markers?</div>
<div><img src="images/custommarkertutorial.png"></div>
<div>pretty</div>
<div>also</div>
<div>RATS</div>
<div><img src="ratmap/custommarker.png"></div>
<div><em>L.icon()</em> to define the icon we're going to use for the rodent points</div>
<div><em>iconUrl</em> is the path to the image file</div>
<div><em>iconSize</em> is a two-number array of the pixel width and height of the icon</div>
<div><em> L.geoJson</em> has been passed an options object, a <em>pointToLayer</em> function</div>
<div>accepts two arguments: the GeoJSON <em>feature</em>, and a LatLng object representing its <em>location</em></div>
<div><img src="ratmap/custommarker.png"></div>
<div>What about interaction?</div>
<div>don't worry, leaflet's got this</div>
<div>before returning the Marker in pointToLayer use <em>bindPopup()</em> method to enable the popup on click</div>
<div>DONE</div>
<div><img src="ratmap/popup.png"></div>
<div>Polygon and line GeoJSON data is added in the same basic way as points. </div>
<div>(In fact, all three could be loaded from a single GeoJSON file.) </div>
<div>We just repeat the step from a few examples back, using neighborhoods.geojson this time</div>
<div><img src="ratmap/poly.png"></div>
<div>but.. but.. two asynchronous <em>$.getJSON</em> requests??</div>
<div>we don't want rodents drawing first, underneath the neighborhoods</div>
<div>Leaflet is clever: the map consists of several panes</div>
<div>paths will always be drawn in overlay pane</div>
<div>cool. now let's turn these paths into a choropleth layer</div>
<div>Remember colorbrewer?</div>
<div><img src="ratmap/polycolor.png"></div>
<div>we now pass an <em>options object</em> to the neighborhoods GeoJSON layer</div>
<div>a style function and</div>
<div>onEachFeature() which uses</div>
<div>bindPopup()</div>
<div><img src="ratmap/polycolor.png"></div>
<div>Combining style with <em>onEachFeature</em> accomplishes something similar to what <em>pointToLayer</em> did for the rodents</div>
<div> <em>style</em> provides some instructions for how to turn the GeoJSON feature into a map layer</div>
<div> and <em>onEachFeature</em> provides some instructions for what to do with that layer</div>
<div>Utilize plug-ins!</div>
<div><img src="ratmap/cluster.png"></div>
<div>You can figure it out.</div>
<div>Go get'em Tiger!</div>
<div>Special thanks to Maptime Boston!</div>
<div>For carto-curious --> <a href="http://maptime.io/lessons-resources/">#maptime</a> </div>
<div>Presentation made with <a href="http://www.macwright.org/big/">big</a></div>
<div><a href="http://bl.ocks.org/awoodruff/raw/5de3553bb1f1b0c5f90d/">Check it</a></div>