forked from unicef-polymer/etools-leaflet-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-core.html
781 lines (686 loc) · 21.4 KB
/
leaflet-core.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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="leaflet-import.html">
<!--
element which defines a leaflet map (<a href="http://leafletjs.com/reference.html#map">Leaflet Reference</a>).
##### Example
<leaflet-map> </leaflet-map>
This simple example will show a map of the world. It is pan and zoomable.
##### Example
<leaflet-map latitude="78.8" longitude="-96" zoom="5">
<leaflet-marker latitude="51.5" longitude="-0.10" title="Some title">
<b>Popup text</b>
</leaflet-marker>
</leaflet-map>
This code will zoom in on the specified latitude and longitude coordinates. Further, it will
add a marker with a popup text.
##### Example: Add markers & circles
<leaflet-map longitude="77.2" latitude="28.4" zoom="12">
<leaflet-marker longitude="77.2" latitude="28.4">
Marker
</leaflet-marker>
<leaflet-circle longitude="77.2" latitude="28.4" radius="300">
Circle
</leaflet-circle>
</leaflet-map>
@element leaflet-map
@blurb element which defines a leaflet map. Most options are supported as attributes.
@homepage https://leaflet-extras.github.io/leaflet-map/
@demo https://leaflet-extras.github.io/leaflet-map/demo.html
-->
<link rel="import" href="styles/leaflet-styles.html">
<dom-module id="leaflet-map">
<template>
<style include="leaflet-styles">
:host {
display: block;
}
:host #map {
height: 100%;
width: 100%
}
</style>
<div id="map"></div>
<slot name="markers" id="markers"></slot>
</template>
<script>
'use strict';
class LeafletMap extends Polymer.Element {
static get is() { return 'leaflet-map'; }
/**
* Fired when the user clicks (or taps) the marker.
*
* @event click
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the user double-clicks (or double-taps) the marker.
*
* @event dblclick
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the user pushes the mouse button on the marker.
*
* @event mousedown
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the mouse enters the marker.
*
* @event mouseover
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the mouse leaves the marker.
*
* @event mouseout
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the user right-clicks on the marker.
*
* @event contextmenu
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the user focuses the map either by tabbing to it or clicking/panning.
*
* @event focus
*/
/**
* Fired when the map looses focus.
*
* @event blur
*/
/**
* Fired before mouse click on the map (sometimes useful when you want something to happen on click before any existing click handlers start running).
*
* @event preclick
* @type MouseEvent
* @param {LatLng} latlng The geographical point where the mouse event occured.
* @param {Point} layerPoint Pixel coordinates of the point where the mouse event occured relative to the map layer.
* @param {Point} containerPoint Pixel coordinates of the point where the mouse event occured relative to the map сontainer.
* @param {DOMMouseEvent} originalEvent The original DOM mouse event fired by the browser.
*/
/**
* Fired when the map is initialized (when its center and zoom are set for the first time).
*
* @event load
*/
/**
* Fired when the map is destroyed with remove method.
*
* @event unload
*/
/**
* Fired when the map needs to redraw its content (this usually happens on map zoom or load). Very useful for creating custom overlays.
*
* @event viewreset
*/
/**
* Fired when the view of the map starts changing (e.g. user starts dragging the map).
*
* @event movestart
*/
/**
* Fired on any movement of the map view.
*
* @event move
*/
/**
* Fired when the view of the map ends changed (e.g. user stopped dragging the map).
*
* @event moveend
*/
/**
* Fired when the user starts dragging the marker.
*
* @event dragstart
*/
/**
* Fired repeatedly while the user drags the marker.
*
* @event drag
*/
/**
* Fired when the user stops dragging the marker.
*
* @event dragend
* @type DragEndEvent
* @param {number} distance The distance in pixels the draggable element was moved by.
*/
/**
* Fired when the map zoom is about to change (e.g. before zoom animation).
*
* @event zoomstart
*/
/**
* Fired when the map zoom changes.
*
* @event zoomend
*/
/**
* Fired when the number of zoomlevels on the map is changed due to adding or removing a layer.
*
* @event zoomlevelschange
*/
/**
* Fired when the map is resized.
*
* @event resize
* @type ResizeEvent
* @param {Point} oldSize The old size before resize event.
* @param {Point} newSize The new size after the resize event.
*/
/**
* Fired when the map starts autopanning when opening a popup.
*
* @event autopanstart
*/
/**
* Fired when a new layer is added to the map.
*
* @event layeradd
* @type LayerEvent
* @param {ILayer} layer The layer that was added or removed.
*/
/**
* Fired when some layer is removed from the map.
*
* @event layerremove
* @type LayerEvent
* @param {ILayer} layer The layer that was added or removed.
*/
/**
* Fired when the base layer is changed through the layer control.
*
* @event baselayerchange
* @type LayerEvent
* @param {ILayer} layer The layer that was added or removed.
*/
/**
* Fired when an overlay is selected through the layer control.
*
* @event overlayadd
* @type LayerEvent
* @param {ILayer} layer The layer that was added or removed.
*/
/**
* Fired when an overlay is deselected through the layer control.
*
* @event overlayremove
* @type LayerEvent
* @param {ILayer} layer The layer that was added or removed.
*/
/**
* Fired when geolocation (using the locate method) went successfully.
*
* @event locationfound
* @type LocationEvent
* @param {LatLng} latlng Detected geographical location of the user.
* @param {LatLngBounds} bounds Geographical bounds of the area user is located in (with respect to the accuracy of location).
* @param {Number} accuracy Accuracy of location in meters.
* @param {Number} altitude Height of the position above the WGS84 ellipsoid in meters.
* @param {Number} altitudeAccuracy Accuracy of altitude in meters.
* @param {Number} heading The direction of travel in degrees counting clockwise from true North.
* @param {Number} speed Current velocity in meters per second.
* @param {Number} timestamp The time when the position was acquired.
*/
/**
* Fired when geolocation (using the locate method) failed.
*
* @event locationerror
* @type ErrorEvent
* @param {string} message Error message.
* @param {number} code Error code (if applicable).
*/
/**
* Fired when a popup bound to the marker is open.
*
* @event popupopen
* @type PopupEvent
* @param {Popup} popup The popup that was opened or closed.
*/
/**
* Fired when a popup bound to the marker is closed.
*
* @event popupclose
* @type PopupEvent
* @param {Popup} popup The popup that was opened or closed.
*/
static get properties() {
return {
/**
* The `latitude` attribute sets the map center.
*
* @attribute latitude
* @type number
*/
latitude: {
type: Number,
value: 51,
reflectToAttribute: true,
notify: true,
observer: '_viewChanged'
},
/**
* The `longitude` attribute sets the map center.
*
* @attribute longitude
* @type number
*/
longitude: {
type: Number,
value: 0,
reflectToAttribute: true,
notify: true,
observer: '_viewChanged'
},
/**
* The `zoom` attribute sets the map zoom.
*
* @attribute zoom
* @type number
*/
zoom: {
type: Number,
value: -1,
reflectToAttribute: true,
notify: true,
observer: '_viewChanged'
},
/**
* The `min-zoom` attribute sets the minimum zoom level of the map. Overrides any minZoom set on map layers.
*
* @attribute min-zoom
* @type number
*/
minZoom: {
type: Number,
value: 0
},
/**
* The `maxZoom` attribute sets the maximum zoom level of the map. This overrides any maxZoom set on map layers.
*
* @attribute max-zoom
* @type number
*/
maxZoom: {
type: Number,
value: 9007199254740992
},
/**
* The `no-dragging` attribute disables whether the map is draggable with mouse/touch or not.
*
* @attribute no-dragging
* @type bool
*/
noDragging: {
type: Boolean,
value: false
},
/**
* The `no-touch-zoom` attribute disables whether the map can be zoomed by touch-dragging with two fingers.
*
* @attribute no-touch-zoom
* @type bool
*/
noTouchZoom: {
type: Boolean,
value: false
},
/**
* The `no-scroll-wheel-zoom` attribute disables whether the map can be zoomed by using the mouse wheel. If passed 'center', it will zoom to the center of the view regardless of where the mouse was.
*
* @attribute no-scroll-wheel-zoom
* @type bool
*/
noScrollWheelZoom: {
type: Boolean,
value: true
},
/**
* The `no-double-click-zoom` attribute disables the whether the map can be zoomed in by double clicking on it and zoomed out by double clicking while holding shift. If passed 'center', double-click zoom will zoom to the center of the view regardless of where the mouse was.
*
* @attribute no-double-click-zoom
* @type bool
*/
noDoubleClickZoom: {
type: Boolean,
value: false
},
/**
* The `no-box-zoom` attribute disable the whether the map can be zoomed to a rectangular area specified by dragging the mouse while pressing shift.
*
* @attribute no-box-zoom
* @type bool
*/
noBoxZoom: {
type: Boolean,
value: false
},
/**
* The `no-tap` attribute disables mobile hacks for supporting instant taps (fixing 200ms click delay on iOS/Android) and touch holds (fired as contextmenu events).
*
* @attribute no-tap
* @type bool
*/
noTap: {
type: Boolean,
value: false
},
/**
* The `tap-tolerance` attribute sets the max number of pixels a user can shift his finger during touch for it to be considered a valid tap.
*
* @attribute tap-tolerance
* @type number
*/
tapTolerance: {
type: Number,
value: 15
},
/**
* The `no-track-resize` attribute disables whether the map automatically handles browser window resize to update itself.
*
* @attribute no-track-resize
* @type bool
*/
noTrackResize: {
type: Boolean,
value: false
},
/**
* The `world-copy-jump` attribute sets whether the map tracks when you pan to another "copy" of the world and seamlessly jumps to the original one so that all overlays like markers and vector layers are still visible.
*
* @attribute world-copy-jump
* @type bool
*/
worldCopyJump: {
type: Boolean,
value: false
},
/**
* The `no-close-popup-on-click` attribute disables whether popups are closed when user clicks the map.
*
* @attribute no-close-popup-on-click
* @type bool
*/
noClosePopupOnClick: {
type: Boolean,
value: false
},
/**
* The `no-bounce-at-zoom-limits` attribute disables whether the map to zoom beyond min/max zoom and then bounce back when pinch-zooming.
*
* @attribute no-bounce-at-zoom-limits
* @type bool
*/
noBounceAtZoomLimits: {
type: Boolean,
value: false
},
/**
* The `no-keyboard` attribute disables whether the map is focusable and allows users to navigate the map with keyboard arrows and +/- keys.
*
* @attribute no-keyboard
* @type bool
*/
noKeyboard: {
type: Boolean,
value: false
},
/**
* The `keyboard-pan-offset` attribute sets the amount of pixels to pan when pressing an arrow key.
*
* @attribute keyboard-pan-offset
* @type number
*/
keyboardPanOffset: {
type: Number,
value: 80
},
/**
* The `keyboard-zoom-offset` attribute sets the number of zoom levels to change when pressing + or - key.
*
* @attribute keyboard-zoom-offset
* @type number
*/
keyboardZoomOffset: {
type: Number,
value: 1
},
/**
* The `no-inertia` attribute disables panning of the map will have an inertia effect where the map builds momentum while dragging and continues moving in the same direction for some time. Feels especially nice on touch devices.
*
* @attribute no-inertia
* @type
*/
noInertia: {
type: Boolean,
value: false
},
/**
* The `inertia-deceleration` attribute sets the rate with which the inertial movement slows down, in pixels/second2.
*
* @attribute inertia-deceleration
* @type number
*/
inertiaDeceleration: {
type: Number,
value: 3000
},
/**
* The `inertia-max-speed` attribute sets the max speed of the inertial movement, in pixels/second.
*
* @attribute inertia-max-speed
* @type number
*/
inertiaMaxSpeed: {
type: Number,
value: 1500
},
/**
* The `no-zoom-control` attribute disables the zoom control is added to the map by default.
*
* @attribute no-zoom-control
* @type bool
*/
noZoomControl: {
type: Boolean,
value: false
},
/**
* The `no-attribution-control` attribute disable the attribution control is added to the map by default.
*
* @attribute no-attribution-control
* @type bool
*/
noAttributionControl: {
type: Boolean,
value: false
},
/**
* The `zoom-animation-threshold` attribute sets the maximum number of zoom level differences that still use animation
*
* @attribute zoom-animation-threshold
* @type number
*/
zoomAnimationThreshold: {
type: Number,
value: 4
},
/**
* If set, the map is zoomed such that all elements in it are visible
*
* @attribute fit-to-markers
* @type boolean
* @default false
*/
fitToMarkers: {
type: Boolean,
value: false,
observer: '_fitToMarkers'
},
/**
* reference to the leaflet map
*
* @property map
* @type L.map
*/
map: {
type: Object
}
};
}
ready() {
super.ready();
// TODO09: Workaround for removed domReady event
var me = this;
setTimeout(function() {me.domReady();}, 1);
}
guessLeafletImagePath() {
if (L.Icon.Default.imagePath) {
return;
}
L.Icon.Default.imagePath = this.resolveUrl('../leaflet/dist/images');
}
domReady() {
this.guessLeafletImagePath();
var map = L.map(this.$.map, {
minZoom: this.minZoom,
maxZoom: this.maxZoom,
dragging: !this.noDragging,
touchZoom: !this.noTouchZoom,
scrollWheelZoom: !this.noScrollWheelZoom,
doubleClickZoom: !this.noDoubleClickZoom,
boxZoom: !this.noBoxZoom,
tap: !this.noTap,
tapTolerance: this.tapTolerance,
trackResize: !this.noTrackResize,
worldCopyJump: this.worldCopyJump,
closePopupOnClick: !this.noClosePopupOnClick,
bounceAtZoomLimits: !this.noBounceAtZoomLimits,
keyboard: !this.noKeyboard,
keyboardPanOffset: this.keyboardPanOffset,
keyboardZoomOffset: this.keyboardZoomOffset,
inertia: !this.noInertia,
inertiaDeceleration: this.inertiaDeceleration,
inertiaMaxSpeed: this.inertiaMaxSpeed,
zoomControl: !this.noZoomControl,
attributionControl: !this.noAttributionControl,
zoomAnimationThreshold: this.zoomAnimationThreshold
});
this.map = map;
// fire an event for when this.map is defined and ready.
// (needed for components that talk to this.map directly)
this.dispatchEvent(new CustomEvent('map-ready'));
// forward events
map.on('click dblclick mousedown mouseup mouseover mouseout mousemove contextmenu focus blur preclick load unload viewreset movestart move moveend dragstart drag dragend zoomstart zoomend zoomlevelschange resize autopanstart layeradd layerremove baselayerchange overlayadd overlayremove locationfound locationerror popupopen popupclose', function(e) {
this.dispatchEvent(new CustomEvent(e.type, e));
}, this);
// set map view after registering events so viewreset and load events can be caught
map.setView([this.latitude, this.longitude], this.zoom);
// update attributes
map.on('moveend', function(e) {
this._ignoreViewChange = true;
this.longitude = map.getCenter().lng;
this.latitude = map.getCenter().lat;
this._ignoreViewChange = false;
}, this);
map.on('zoomend', function(e) {
this.zoom = map.getZoom();
}, this);
if (this.zoom == -1) {
this.map.fitWorld();
}
// add a default layer if there are no layers defined
var defaultLayerRequired = true;
for (var i = 0; i < this.children.length; i++) {
var e = this.children[i];
if (e.isLayer && e.isLayer()) {
defaultLayerRequired = false;
}
}
if (defaultLayerRequired) {
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '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>',
maxZoom: 18
}).addTo(this.map);
}
this.registerMapOnChildren();
this._mutationObserver = new MutationObserver(this.registerMapOnChildren.bind(this));
this._mutationObserver.observe(this, { childList: true });
}
disconnectedCallback() {
this._mutationObserver.disconnect();
}
_viewChanged(newValue, oldValue) {
if (this.map && !this._ignoreViewChange) {
setTimeout(function() {
this.map.setView(L.latLng(this.latitude, this.longitude), this.zoom);
}.bind(this), 1);
}
}
registerMapOnChildren() {
for (var i = 0; i < this.children.length; i++) {
this.children[i].container = this.map;
}
this._fitToMarkers();
}
_fitToMarkers() {
if ( this.map && this.fitToMarkers) {
var bounds = [];
for( var i = 0, f; f = this.children[i]; i++ ) {
if ( f.latitude && f.longitude ) {
bounds.push( [f.latitude, f.longitude] );
}
}
if ( bounds.length > 0 ) {
this.map.fitBounds( bounds );
this.map.invalidateSize();
}
}
}
/**
* Returns a GeoJSON including all the features of the map
*
* @method toGeoJSON
*/
toGeoJSON() {
var geoJSON = {
'type': 'FeatureCollection',
'features': []
};
for( var i = 0, f; f = this.features[i]; i++ ) {
geoJSON.features.push( f.feature.toGeoJSON() );
}
return geoJSON;
}
}
window.customElements.define(LeafletMap.is, LeafletMap);
</script>
</dom-module>