forked from unicef-polymer/etools-leaflet-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-marker.html
770 lines (670 loc) · 19.5 KB
/
leaflet-marker.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
<link rel="import" href="leaflet-core.html">
<link rel="import" href="leaflet-popup.html">
<!--
Element which defines an icon template for markers (<a href="http://leafletjs.com/reference.html#icon">Leaflet Reference</a>).
##### Example
<leaflet-icon id="myicon"
icon-url="https://stendhalgame.org/images/mapmarker/me.png">
</leaflet-icon>
@element leaflet-icon
@blurb element which defines an icon template for markers.
@demo https://leaflet-extras.github.io/leaflet-map/demo.html
@homepage https://leaflet-extras.github.io/leaflet-map/
-->
<dom-module id="leaflet-icon">
<style>
:host {display: none;}
</style>
<template>
</template>
<script>
'use strict';
class LeafletIcon extends Polymer.Element {
static get is() {return 'leaflet-icon';}
static get properties() {
return {
/**
* The `icon-url` attribute sets the URL to the icon image (absolute or relative to your script path).
*
* @attribute icon-url
* @type string
*/
iconUrl: {
type: String,
observer: '_attributeChanged'
},
/**
* The `icon-retina-url` attribute sets the URL to a retina sized version of the icon image (absolute or relative to your script path). Used for Retina screen devices.
*
* @attribute icon-retina-url
* @type string
*/
iconRetinaUrl: {
type: String,
observer: '_attributeChanged'
},
/**
* The `icon-width` attribute sets the size of the icon image in pixels.
*
* @attribute icon-width
* @type number
*/
iconWidth: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `icon-height` attribute sets the size of the icon image in pixels.
*
* @attribute icon-height
* @type number
*/
iconHeight: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `icon-anchor-x` attribute sets the coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker's geographical location. Centered by default if size is specified, also can be set in CSS with negative margins.
*
* @attribute icon-anchor-x
* @type number
*/
iconAnchorX: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `icon-anchor-y` attribute sets the coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker's geographical location. Centered by default if size is specified, also can be set in CSS with negative margins.
*
* @attribute icon-anchor-y
* @type number
*/
iconAnchorY: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `shadow-url` attribute sets the URL to the icon shadow image. If not specified, no shadow image will be created.
*
* @attribute shadow-url
* @type string
*/
shadowUrl: {
type: String,
observer: '_attributeChanged'
},
/**
* The `shadow-retina-url` attribute sets the URL to the retina sized version of the icon shadow image. If not specified, no shadow image will be created. Used for Retina screen devices.
*
* @attribute shadow-retina-url
* @type string
*/
shadowRetinaUrl: {
type: String,
observer: '_attributeChanged'
},
/**
* The `shadow-width` attribute sets the size of the shadow image in pixels.
*
* @attribute shadow-width
* @type number
*/
shadowWidth: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `shadow-height` attribute sets the size of the shadow image in pixels.
*
* @attribute shadow-height
* @type number
*/
shadowHeight: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `shadow-anchor-x` attribute sets the coordinates of the "tip" of the shadow (relative to its top left corner) (the same as iconAnchor if not specified).
*
* @attribute shadow-anchor-x
* @type number
*/
shadowAnchorX: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `shadow-anchor-y` attribute sets the coordinates of the "tip" of the shadow (relative to its top left corner) (the same as iconAnchor if not specified).
*
* @attribute shadow-anchor-y
* @type number
*/
shadowAnchorY: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `popup-anchor-x` attribute sets the coordinates of the point from which popups will "open", relative to the icon anchor.
*
* @attribute popup-anchor-x
* @type number
*/
popupAnchorX: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `popupanchory` attribute sets the coordinates of the point from which popups will "open", relative to the icon anchor.
*
* @attribute popup-anchor-y
* @type number
*/
popupAnchorY: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `class-name` attribute sets a custom class name to assign to both icon and shadow images. Empty by default.
*
* @attribute class-name
* @type string
*/
className: {
type: String,
value: '',
observer: '_attributeChanged'
},
icon_: null
};
}
getIcon() {
if (this.icon_) {
return this.icon_;
}
var icon = {
iconUrl: this.iconUrl,
iconRetinaUrl: this.iconRetinaUrl,
shadowUrl: this.shadowUrl,
shadowRetinaUrl: this.shadowRetinaUrl,
className: this.className
};
if (this.iconWidth && this.iconHeight) {
icon.iconSize = L.point(this.iconWidth, this.iconHeight);
}
if (this.iconAnchorX && this.iconAnchorY) {
icon.iconAnchor = L.point(this.iconAnchorX, this.iconAnchorY);
}
if (this.shadowWidth && this.shadowHeight) {
icon.shadowSize = L.point(this.shadowWidth, this.shadowHeight);
}
if (this.shadowAnchorX && this.shadowAnchorY) {
icon.shadowAnchor = L.point(this.shadowAnchorX, this.shadowAnchorY);
}
if (this.popupAnchorX && this.popupAnchorY) {
icon.popupAnchor = L.point(this.popupAnchorX, this.popupAnchorY);
}
this.icon_ = L.icon(icon);
return this.icon_;
}
_attributeChanged() {
this.icon_ = null;
}
}
window.customElements.define(LeafletIcon.is, LeafletIcon);
</script>
</dom-module>
<!--
Element which defines an divicon template for markers (<a href="http://leafletjs.com/reference.html#divicon">Leaflet Reference</a>).
##### Example
<leaflet-divicon id="myicon" classname="name">
<a href="https://leaflet-extras.github.io/leaflet-map/demo.html">Demo</a>
</leaflet-icon>
@element leaflet-divicon
@blurb element which defines an divicon template for markers.
@demo https://leaflet-extras.github.io/leaflet-map/demo.html
@homepage https://leaflet-extras.github.io/leaflet-map/
-->
<dom-module id="leaflet-divicon">
<style>
:host {display: none;}
</style>
<template>
</template>
<script>
'use strict';
class LeafletDivicon extends Polymer.Element {
static get is() {return 'leaflet-divicon';}
static get properties() {
return {
/**
* The `icon-width` attribute sets the size of the icon image in pixels.
*
* @attribute icon-width
* @type number
*/
iconWidth: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `icon-height` attribute sets the size of the icon image in pixels.
*
* @attribute icon-height
* @type number
*/
iconHeight: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `icon-anchor-x` attribute sets the coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker's geographical location. Centered by default if size is specified, also can be set in CSS with negative margins.
*
* @attribute icon-anchor-x
* @type number
*/
iconAnchorX: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `icon-anchor-y` attribute sets the coordinates of the "tip" of the icon (relative to its top left corner). The icon will be aligned so that this point is at the marker's geographical location. Centered by default if size is specified, also can be set in CSS with negative margins.
*
* @attribute icon-anchor-y
* @type number
*/
iconAnchorY: {
type: Number,
observer: '_attributeChanged'
},
/**
* The `class-name` attribute sets a custom class name to assign to both icon and shadow images. Empty by default.
*
* @attribute class-name
* @type string
*/
className: {
type: String,
value: '',
observer: '_attributeChanged'
},
icon_: null
};
}
getIcon() {
if (this.icon_) {
return this.icon_;
}
var icon = {
className: this.className,
html: Polymer.dom(this).innerHTML
};
if (this.iconWidth && this.iconHeight) {
icon.iconSize = L.point(this.iconWidth, this.iconHeight);
}
if (this.iconAnchorX && this.iconAnchorY) {
icon.iconAnchor = L.point(this.iconAnchorX, this.iconAnchorY);
}
this.icon_ = L.divIcon(icon);
return this.icon_;
}
_attributeChanged() {
this.icon_ = null;
}
}
window.customElements.define(LeafletDivicon.is, LeafletDivicon);
</script>
</dom-module>
<!--
Element which defines a maker (<a href="http://leafletjs.com/reference.html#marker">Leaflet Reference</a>).
##### Example
<leaflet-marker latitude="51.5" longitude="-0.10" title="Some title">
<b>Popup text</b>
</leaflet-marker>
##### Example
<leaflet-icon id="myicon" iconurl="https://stendhalgame.org/images/mapmarker/me.png"></leaflet-icon>
<leaflet-marker latitude="51.5" longitude="-0.10" title="Some title" icon="myicon">
</leaflet-marker>
@element leaflet-marker
@blurb element which defines a marker. The content is used as popup window, unless it is empty.
@demo https://leaflet-extras.github.io/leaflet-map/demo.html
@homepage https://leaflet-extras.github.io/leaflet-map/
-->
<dom-module id="leaflet-marker">
<style>
:host {display: none;}
</style>
<template>
</template>
<script>
'use strict';
class LeafletMarker extends leafletMap.Mixins.LeafletPopupContent(Polymer.Element) {
static get is() {return 'leaflet-marker';}
/**
* 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
*/
/**
* 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 marker is moved via setLatLng. New coordinate include in event arguments.
*
* @event move
* @type
*/
/**
* Fired when the marker is added to the map.
*
* @event add
* @type
*/
/**
* Fired when the marker is removed from the map.
*
* @event remove
*/
/**
* 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 positions of the marker.
*
* @attribute latitude
* @type number
*/
latitude: {
type: Number,
value: null,
reflectToAttribute: true,
notify: true,
observer: '_positionChanged'
},
/**
* The `longitude` attribute sets the positions of the marker.
*
* @attribute longitude
* @type number
*/
longitude: {
type: Number,
value: null,
reflectToAttribute: true,
notify: true,
observer: '_positionChanged'
},
/**
* The `icon` attribute sets the Icon class to use for rendering the marker.
* This attribute may be refer to an id-attribute of an leaflet-icon-element,
* contain json syntax or it be assigned an instance of L.icon.
* See Icon documentation for details on how to customize the marker icon. Set to new L.Icon.Default() by default.
*
* @attribute icon
* @type string, json or L.icon
*/
icon: {
type: Object,
observer: '_iconChanged'
},
/**
* The `no-clickable` disables emitting mouse events and will act as a part of the underlying map.
*
* @attribute no-clickable
* @type bool
*/
noClickable: {
type: Boolean,
value: false
},
/**
* The `draggable` attribute sets the whether the marker is draggable with mouse/touch or not.
*
* @attribute draggable
* @type bool
*/
draggable: {
type: Boolean,
value: false
},
/**
* The `no-keyboard` attribute disables whether the marker can be tabbed to with a keyboard and clicked by pressing enter.
*
* @attribute no-keyboard
* @type bool
*/
noKeyboard: {
type: Boolean,
value: false
},
/**
* The `title` attribute sets the text for the browser tooltip that appear on marker hover (no tooltip by default).
*
* @attribute title
* @type string
*/
title: {
type: String,
value: ''
},
/**
* The `alt` attribute sets the text for the alt attribute of the icon image (useful for accessibility).
*
* @attribute alt
* @type string
*/
alt: {
type: String,
value: ''
},
/**
* The `z-index-offset` attribute sets the zIndexOffset. By default, marker images zIndex is set automatically based on its latitude
*
* @attribute z-index-offset
* @type number
*/
zIndexOffset: {
type: Number,
value: 0,
observer: '_zIndexOffsetChanged'
},
/**
* The `opacity` attribute sets the opacity of the marker.
*
* @attribute opacity
* @type number
*/
opacity: {
type: Number,
value: 1.0,
observer: '_opacityChanged'
},
/**
* The `rise-on-hover` attribute sets the whether the marker will get on top of others when you hover the mouse over it.
*
* @attribute rise-on-hover
* @type bool
*/
riseOnHover: {
type: Boolean,
value: false
},
/**
* The `rise-offset` attribute sets the z-index offset used for the riseOnHover feature.
*
* @attribute rise-offset
* @type number
*/
riseoffset: {
type: Number,
value: 250
},
container: {
type: Object,
observer: '_containerChanged'
},
feature: {
type: Object
},
observer_: {
type: Object
}
};
}
_containerChanged() {
if (this.container) {
var feature = L.marker([this.latitude, this.longitude], {
clickable: !this.noClickable,
draggable: this.draggable,
keyboard: !this.noKeyboard,
title: this.title,
alt: this.alt,
zIndexOffset: this.zIndexOffset,
opacity: this.opacity,
riseOnHover: this.riseOnHover,
riseOffset: this.riseOffset
});
this.feature = feature;
this._iconChanged();
// forward events
feature.on('click dblclick mousedown mouseover mouseout contextmenu dragstart drag dragend move add remove popupopen popupclose', function(e) {
this.dispatchEvent(new CustomEvent(e.type, e));
}, this);
this.updatePopupContent();
this.feature.addTo(this.container);
}
}
_iconChanged() {
// icon support
var iconOption;
if (this.icon) {
if (typeof this.icon == 'string') {
var iconElement = document.getElementById(this.icon);
if (iconElement != null) {
if (iconElement.getIcon) {
iconOption = iconElement.getIcon();
}
} else {
try {
iconOption = L.icon(JSON.parse(this.icon));
} catch (e) {
iconOption = new L.Icon.Default();
}
}
}
if (typeof this.icon == 'object') {
if (this.icon.options) {
iconOption = this.icon;
} else {
iconOption = L.icon(this.icon);
}
}
}
if (!iconOption) {
iconOption = new L.Icon.Default();
}
if (this.feature) {
this.feature.setIcon(iconOption);
}
}
_positionChanged() {
if (this.feature) {
this.feature.setLatLng(L.latLng(this.latitude, this.longitude));
}
}
_zIndexOffsetChanged() {
if (this.feature) {
this.feature.setZIndexOffset(this.zIndexOffset);
}
}
_opacityChanged() {
if (this.feature) {
this.feature.setOpacity(this.opacity);
}
}
disconnectedCallback() {
if (this.container && this.feature) {
this.container.removeLayer(this.feature);
}
}
}
window.customElements.define(LeafletMarker.is, LeafletMarker);
</script>
</dom-module>