This repository has been archived by the owner on Mar 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-tile-layer.js
98 lines (85 loc) · 2.47 KB
/
leaflet-tile-layer.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
import { Element as PolymerElement } from '../../@polymer/polymer/polymer-element.js';
import { TileLayer } from '../../leaflet/src/layer/tile/TileLayer.js';
import { TileLayerWMS } from '../../leaflet/src/layer/tile/TileLayer.WMS.js';
export class LeafletTileLayer extends PolymerElement {
static get properties() {
return {
map: {
type: Object,
observer: '_mapSet'
},
base: Boolean,
url: {
type: String,
value: "//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
observer: '_urlChanged',
reflectToAttribute: true
},
format: {
type: String,
observer: '_formatChanged',
reflectToAttribute: true
},
layers: {
type: String,
observer: '_layersChanged'
},
minZoom: Number,
maxZoom: Number,
attribution: String
}
}
constructor() {
super();
}
connectedCallback() {
super.connectedCallback();
}
_urlChanged () {
console.log('url changed', this.url);
if (this.leafletLayer) this.leafletLayer.setUrl(this.url);
}
_layersChanged () {
console.log('layers changed', this.layers);
if (this.leafletLayer && this.leafletLayer instanceof TileLayerWMS) this.leafletLayer.setParams({ layers: this.layers });
}
_formatChanged () {
console.log('format changed', this.format);
if (this.leafletLayer && this.map) {
this.leafletLayer.removeFrom(this.map);
}
let options = {
minZoom: this.minZoom,
maxZoom: this.maxZoom,
attribution: this.attribution
};
switch (this.format) {
case 'XYZ':
this.leafletLayer = new TileLayer(this.url, options);
break;
case 'WMS':
this.leafletLayer = new TileLayerWMS(this.url, Object.assign(options, {
layers: this.layers,
format: 'image/png',
transparent: false,
hints: 'quality'
}));
break;
default:
console.error('Invalid leaflet-tile-layer format: ', this.format);
return;
}
if (this.map) this.leafletLayer.addTo(this.map);
else console.log('map not yet set');
}
_mapSet() {
console.log('map set');
if (this.leafletLayer && !this.map.hasLayer(this.leafletLayer)) {
console.log('no layer yet, adding');
this.leafletLayer.addTo(this.map);
} else {
console.log('layer object not set / layer exists, skipping');
}
}
}
customElements.define('leaflet-tile-layer', LeafletTileLayer);