-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
map.js
720 lines (612 loc) · 20.5 KB
/
map.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
'use strict';
var Canvas = require('../util/canvas');
var util = require('../util/util');
var browser = require('../util/browser');
var Evented = require('../util/evented');
var Style = require('../style/style');
var AnimationLoop = require('../style/animation_loop');
var GLPainter = require('../render/painter');
var Transform = require('../geo/transform');
var Hash = require('./hash');
var Handlers = require('./handlers');
var Easings = require('./easings');
var LatLng = require('../geo/lat_lng');
var LatLngBounds = require('../geo/lat_lng_bounds');
var Point = require('point-geometry');
var Attribution = require('./control/attribution');
/**
* Creates a map instance.
* @class mapboxgl.Map
* @param {Object} options
* @param {String} options.container HTML element to initialize the map in (or element id as string)
* @param {Number} [options.minZoom=0] Minimum zoom of the map
* @param {Number} [options.maxZoom=20] Maximum zoom of the map
* @param {Object} options.style Map style and data source definition (either a JSON object or a JSON URL), described in the [style reference](https://mapbox.com/mapbox-gl-style-spec/)
* @param {Boolean} [options.hash=false] If `true`, the map will track and update the page URL according to map position
* @param {Boolean} [options.interactive=true] If `false`, no mouse, touch, or keyboard listeners are attached to the map, so it will not respond to input
* @param {Array} options.classes Style class names with which to initialize the map
* @param {Boolean} [options.failIfMajorPerformanceCaveat=false] If `true`, map creation will fail if the implementation determines that the performance of the created WebGL context would be dramatically lower than expected.
* @example
* var map = new mapboxgl.Map({
* container: 'map',
* center: [37.772537, -122.420679],
* zoom: 13,
* style: style_object,
* hash: true
* });
*/
var Map = module.exports = function(options) {
options = this.options = util.inherit(this.options, options);
this.animationLoop = new AnimationLoop();
this.transform = new Transform(options.minZoom, options.maxZoom);
if (options.maxBounds) {
var b = LatLngBounds.convert(options.maxBounds);
this.transform.latRange = [b.getSouth(), b.getNorth()];
this.transform.lngRange = [b.getWest(), b.getEast()];
}
util.bindAll([
'_forwardStyleEvent',
'_forwardSourceEvent',
'_forwardLayerEvent',
'_forwardTileEvent',
'_onStyleLoad',
'_onStyleChange',
'_onSourceAdd',
'_onSourceRemove',
'_onSourceUpdate',
'update',
'render'
], this);
this._setupContainer();
this._setupPainter();
this.handlers = options.interactive && new Handlers(this);
this._hash = options.hash && (new Hash()).addTo(this);
// don't set position from options if set through hash
if (!this._hash || !this._hash._onHashChange()) {
this.setView(options.center, options.zoom, options.bearing, options.pitch);
}
this.sources = {};
this.stacks = {};
this._classes = {};
this.resize();
if (options.classes) this.setClasses(options.classes);
if (options.style) this.setStyle(options.style);
if (options.attributionControl) this.addControl(new Attribution());
};
util.extend(Map.prototype, Evented);
util.extend(Map.prototype, Easings);
util.extend(Map.prototype, {
options: {
center: [0, 0],
zoom: 0,
bearing: 0,
pitch: 0,
minZoom: 0,
maxZoom: 20,
interactive: true,
hash: false,
attributionControl: true,
failIfMajorPerformanceCaveat: false
},
addControl: function(control) {
control.addTo(this);
return this;
},
/**
* Sets a map position
*
* @param {Array} center Latitude and longitude (passed as `[lat, lng]`)
* @param {Number} zoom Map zoom level
* @param {Number} bearing Map rotation bearing in degrees counter-clockwise from north
* @param {Number} pitch The angle at which the camera is looking at the ground
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setView: function(center, zoom, bearing, pitch) {
this.stop();
var tr = this.transform,
zoomChanged = tr.zoom !== +zoom,
bearingChanged = tr.bearing !== +bearing,
pitchChanged = tr.pitch !== +pitch;
tr.center = LatLng.convert(center);
tr.zoom = +zoom;
tr.bearing = +bearing;
tr.pitch = +pitch;
return this
.fire('movestart')
._move(zoomChanged, bearingChanged, pitchChanged)
.fire('moveend');
},
/**
* Sets a map location
*
* @param {Array} center Latitude and longitude (passed as `[lat, lng]`)
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setCenter: function(center) {
this.setView(center, this.getZoom(), this.getBearing(), this.getPitch());
},
/**
* Sets a map zoom
*
* @param {Number} zoom Map zoom level
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setZoom: function(zoom) {
this.setView(this.getCenter(), zoom, this.getBearing(), this.getPitch());
},
/**
* Sets a map rotation
*
* @param {Number} bearing Map rotation bearing in degrees counter-clockwise from north
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setBearing: function(bearing) {
this.setView(this.getCenter(), this.getZoom(), bearing, this.getPitch());
},
/**
* Sets a map angle
*
* @param {Number} pitch The angle at which the camera is looking at the ground
* @fires movestart
* @fires moveend
* @returns {Map} `this`
*/
setPitch: function(pitch) {
this.setView(this.getCenter(), this.getZoom(), this.getBearing(), pitch);
},
/**
* Get the current view geographical point (as `LatLng` object)
* @returns {Array} `[lat, lng]`
*/
getCenter: function() { return this.transform.center; },
/**
* Get the current zoom
* @returns {Number}
*/
getZoom: function() { return this.transform.zoom; },
/**
* Get the current bearing in degrees
* @returns {Number}
*/
getBearing: function() { return this.transform.bearing; },
/**
* Get the current angle in degrees
* @returns {Number}
*/
getPitch: function() { return this.transform.pitch; },
/**
* @typedef {Object} [styleOptions]
* @param {Boolean} [styleOptions.transition=true]
*/
/**
* Adds a style class to a map
*
* @param {String} class name of style class
* @param {styleOptions}
* @fires change
* @returns {Map} `this`
*/
addClass: function(klass, options) {
if (this._classes[klass]) return;
this._classes[klass] = true;
if (this.style) this.style._cascade(this._classes, options);
},
/**
* Removes a style class from a map
*
* @param {String} class name of style class
* @param {styleOptions}
* @fires change
* @returns {Map} `this`
*/
removeClass: function(klass, options) {
if (!this._classes[klass]) return;
delete this._classes[klass];
if (this.style) this.style._cascade(this._classes, options);
},
/**
* Helper method to add more than one class
*
* @param {Array} classes An array of class names
* @param {styleOptions}
* @fires change
* @returns {Map} `this`
*/
setClasses: function(klasses, options) {
this._classes = {};
for (var i = 0; i < klasses.length; i++) {
this._classes[klasses[i]] = true;
}
if (this.style) this.style._cascade(this._classes, options);
},
/**
* Check whether a style class is active
*
* @param {String} class Name of style class
* @returns {Boolean}
*/
hasClass: function(klass) {
return !!this._classes[klass];
},
/**
* Return an array of the current active style classes
*
* @returns {Boolean}
*/
getClasses: function() {
return Object.keys(this._classes);
},
/**
* Detect the map's new width and height and resize it.
*
* @returns {Map} `this`
*/
resize: function() {
var width = 0, height = 0;
if (this._container) {
width = this._container.offsetWidth || 400;
height = this._container.offsetHeight || 300;
}
this._canvas.resize(width, height);
this.transform.width = width;
this.transform.height = height;
this.transform._constrain();
this.painter.resize(width, height);
return this
.fire('movestart')
._move()
.fire('resize')
.fire('moveend');
},
/**
* Get the map's geographical bounds
*
* @returns {Object} `LatLngBounds`
*/
getBounds: function() {
return new LatLngBounds(
this.transform.pointLocation(new Point(0, 0)),
this.transform.pointLocation(this.transform.size));
},
/**
* Get pixel coordinates (relative to map container) given a geographical location
*
* @param {Array} [lat, lng]
* @returns {Object} `x` and `y` coordinates
*/
project: function(latlng) {
return this.transform.locationPoint(LatLng.convert(latlng));
},
/**
* Get geographical coordinates given pixel coordinates
*
* @param {Array} [x, y] pixel coordinates
* @returns {Object} `lat` and `long` coordinates
*/
unproject: function(point) {
return this.transform.pointLocation(Point.convert(point));
},
/**
* Get all features at a point ([x, y])
*
* @param {Array} [x, y] pixel coordinates
* @param {Object} params
* @param {Number} [params.radius=0] Optional. Radius in pixels to search in
* @param {String} params.layer Optional. Only return features from a given layer
* @param {String} params.type Optional. Either `raster` or `vector`
* @param {featuresAtCallback} callback function that returns the response
*
* @callback featuresAtCallback
* @param {Object|null} err Error _If any_
* @param {Array} features Displays a JSON array of features given the passed parameters of `featuresAt`
*
* @returns {Map} `this`
*/
featuresAt: function(point, params, callback) {
this.style.featuresAt(point, params, callback);
return this;
},
/**
* Replaces the map's style object
*
* @param {Object} style A style object formatted as JSON
* @returns {Map} `this`
*/
setStyle: function(style) {
if (this.style) {
this.style
.off('load', this._onStyleLoad)
.off('error', this._forwardStyleEvent)
.off('change', this._onStyleChange)
.off('source.add', this._onSourceAdd)
.off('source.remove', this._onSourceRemove)
.off('source.load', this._onSourceUpdate)
.off('source.error', this._forwardSourceEvent)
.off('source.change', this._onSourceUpdate)
.off('layer.add', this._forwardLayerEvent)
.off('layer.remove', this._forwardLayerEvent)
.off('tile.add', this._forwardTileEvent)
.off('tile.remove', this._forwardTileEvent)
.off('tile.load', this.update)
.off('tile.error', this._forwardTileEvent)
._remove();
this.off('rotate', this.style._redoPlacement);
this.off('pitch', this.style._redoPlacement);
}
if (!style) {
this.style = null;
return this;
} else if (style instanceof Style) {
this.style = style;
} else {
this.style = new Style(style, this.animationLoop);
}
this.style
.on('load', this._onStyleLoad)
.on('error', this._forwardStyleEvent)
.on('change', this._onStyleChange)
.on('source.add', this._onSourceAdd)
.on('source.remove', this._onSourceRemove)
.on('source.load', this._onSourceUpdate)
.on('source.error', this._forwardSourceEvent)
.on('source.change', this._onSourceUpdate)
.on('layer.add', this._forwardLayerEvent)
.on('layer.remove', this._forwardLayerEvent)
.on('tile.add', this._forwardTileEvent)
.on('tile.remove', this._forwardTileEvent)
.on('tile.load', this.update)
.on('tile.error', this._forwardTileEvent);
this.on('rotate', this.style._redoPlacement);
this.on('pitch', this.style._redoPlacement);
return this;
},
/**
* Add a source to the map style.
*
* @param id {string} ID of the source. Must not be used by any existing source.
* @param source {Object} source specification, following the
* [Mapbox GL Style Reference](https://www.mapbox.com/mapbox-gl-style-spec/#sources)
* @fires source.add
* @returns {Map} `this`
*/
addSource: function(id, source) {
this.style.addSource(id, source);
return this;
},
/**
* Remove an existing source from the map style.
*
* @param id {string} ID of the source to remove
* @fires source.remove
* @returns {Map} `this`
*/
removeSource: function(id) {
this.style.removeSource(id);
return this;
},
/**
* Return the style source object with the given `id`.
*
* @param id {string} source ID
* @returns {Object}
*/
getSource: function(id) {
return this.style.getSource(id);
},
/**
* Add a layer to the map style. The layer will be inserted before the layer with
* ID `before`, or appended if `before` is omitted.
* @param layer {Layer}
* @param before {string=} ID of an existing layer to insert before
* @fires layer.add
* @returns {Map} `this`
*/
addLayer: function(layer, before) {
this.style.addLayer(layer, before);
this.style._cascade(this._classes);
return this;
},
/**
* Remove the layer with the given `id` from the map. Any layers which refer to the
* specified layer via a `ref` property are also removed.
*
* @param id {string}
* @fires layer.remove
* @returns {Map} `this`
*/
removeLayer: function(id) {
this.style.removeLayer(id);
this.style._cascade(this._classes);
return this;
},
setFilter: function(layer, filter) {
this.style.setFilter(layer, filter);
return this;
},
getFilter: function(layer) {
return this.style.getFilter(layer);
},
setPaintProperty: function(layer, name, value, klass) {
this.style.setPaintProperty(layer, name, value, klass);
this.style._cascade(this._classes);
this.update(true);
return this;
},
getPaintProperty: function(layer, name, klass) {
return this.style.getPaintProperty(layer, name, klass);
},
setLayoutProperty: function(layer, name, value) {
this.style.setLayoutProperty(layer, name, value);
return this;
},
getLayoutProperty: function(layer, name) {
return this.style.getLayoutProperty(layer, name);
},
getContainer: function() {
return this._container;
},
getCanvas: function() {
return this._canvas.getElement();
},
_move: function(zoom, rotate, pitch) {
this.update(zoom).fire('move');
if (zoom) this.fire('zoom');
if (rotate) this.fire('rotate');
if (pitch) this.fire('pitch');
return this;
},
// map setup code
_setupContainer: function() {
var id = this.options.container;
var container = this._container = typeof id === 'string' ? document.getElementById(id) : id;
if (container) container.classList.add('mapboxgl-map');
this._canvas = new Canvas(this, container);
},
_setupPainter: function() {
var gl = this._canvas.getWebGLContext(this.options.failIfMajorPerformanceCaveat);
if (!gl) {
console.error('Failed to initialize WebGL');
return;
}
this.painter = new GLPainter(gl, this.transform);
},
_contextLost: function(event) {
event.preventDefault();
if (this._frameId) {
browser.cancelFrame(this._frameId);
}
},
_contextRestored: function() {
this._setupPainter();
this.resize();
this.update();
},
// Rendering
loaded: function() {
if (this._styleDirty || this._sourcesDirty)
return false;
if (this.style && !this.style.loaded())
return false;
return true;
},
update: function(updateStyle) {
if (!this.style) return this;
this._styleDirty = this._styleDirty || updateStyle;
this._sourcesDirty = true;
this._rerender();
return this;
},
// Call when a (re-)render of the map is required, e.g. when the user panned or zoomed,f or new data is available.
render: function() {
if (this.style && this._styleDirty) {
this._styleDirty = false;
this.style._recalculate(this.transform.zoom);
}
if (this.style && this._sourcesDirty && !this._sourcesDirtyTimeout) {
this._sourcesDirty = false;
this._sourcesDirtyTimeout = setTimeout(function() {
this._sourcesDirtyTimeout = null;
}.bind(this), 50);
this.style._updateSources(this.transform);
}
this.painter.render(this.style, {
debug: this.debug,
vertices: this.vertices,
rotating: this.rotating,
zooming: this.zooming
});
this.fire('render');
if (this.loaded() && !this._loaded) {
this._loaded = true;
this.fire('load');
}
this._frameId = null;
if (!this.animationLoop.stopped()) {
this._styleDirty = true;
}
if (this._sourcesDirty || this._repaint || !this.animationLoop.stopped()) {
this._rerender();
}
return this;
},
remove: function() {
if (this._hash) this._hash.remove();
browser.cancelFrame(this._frameId);
clearTimeout(this._sourcesDirtyTimeout);
this.setStyle(null);
return this;
},
_rerender: function() {
if (this.style && !this._frameId) {
this._frameId = browser.frame(this.render);
}
},
_forwardStyleEvent: function(e) {
this.fire('style.' + e.type, util.extend({style: e.target}, e));
},
_forwardSourceEvent: function(e) {
this.fire(e.type, util.extend({style: e.target}, e));
},
_forwardLayerEvent: function(e) {
this.fire(e.type, util.extend({style: e.target}, e));
},
_forwardTileEvent: function(e) {
this.fire(e.type, util.extend({style: e.target}, e));
},
_onStyleLoad: function(e) {
this.style._cascade(this._classes, {transition: false});
this._forwardStyleEvent(e);
},
_onStyleChange: function(e) {
this.update(true);
this._forwardStyleEvent(e);
},
_onSourceAdd: function(e) {
var source = e.source;
if (source.onAdd)
source.onAdd(this);
this._forwardSourceEvent(e);
},
_onSourceRemove: function(e) {
var source = e.source;
if (source.onRemove)
source.onRemove(this);
this._forwardSourceEvent(e);
},
_onSourceUpdate: function(e) {
this.update();
this._forwardSourceEvent(e);
}
});
util.extendAll(Map.prototype, {
// debug code
_debug: false,
get debug() { return this._debug; },
set debug(value) { this._debug = value; this.update(); },
// show collision boxes
_collisionDebug: false,
get collisionDebug() { return this._collisionDebug; },
set collisionDebug(value) {
this._collisionDebug = value;
for (var i in this.style.sources) {
this.style.sources[i].reload();
}
this.update();
},
// continuous repaint
_repaint: false,
get repaint() { return this._repaint; },
set repaint(value) { this._repaint = value; this.update(); },
// show vertices
_vertices: false,
get vertices() { return this._vertices; },
set vertices(value) { this._vertices = value; this.update(); }
});