-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathleaflet-tilelayer-here.js
More file actions
152 lines (115 loc) · 3.91 KB
/
Copy pathleaflet-tilelayer-here.js
File metadata and controls
152 lines (115 loc) · 3.91 KB
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
L.TileLayer.HERE = L.TileLayer.extend({
options: {
subdomains: '1234',
minZoom: 2,
maxZoom: 18,
// option style: String = 'explore.day'
style: 'explore.day',
// option resource: String = 'base'
resource: 'base',
// option format: String = 'png8'
// Image format to be used (`png8`, `png`, or `jpg`)
format: 'png8',
// option apiKey: String = ''
// Required option. The `apiKey` provided as part of the HERE credentials
apiKey: '',
// option features: String = ''
// Map Features
features: null,
// option mapVersion: String = ''
// Map Version
mapVersion: null,
// option language: String = ''
// Map language
language: null
},
initialize: function initialize(options) {
options = L.setOptions(this, options);
options.tileResolution = 256;
if (L.Browser.retina) {
options.tileResolution = 512;
}
var tileUrl = 'https://maps.hereapi.com/v3/{resource}/mc/{z}/{x}/{y}/{format}?apiKey={apiKey}&style={style}&size={tileResolution}';
var copyrightUrl = 'https://maps.hereapi.com/v3/copyright?apiKey={apiKey}';
if (options.features) {
tileUrl += '&features={features}'
}
if (options.mapVersion) {
tileUrl += '&mv={mapVersion}'
}
if (options.language) {
tileUrl += '&lang={language}'
}
this._attributionUrl = L.Util.template(copyrightUrl, this.options);
L.TileLayer.prototype.initialize.call(this, tileUrl, options);
this._attributionText = '';
},
onAdd: function onAdd(map) {
L.TileLayer.prototype.onAdd.call(this, map);
if (!this._attributionBBoxes) {
this._fetchAttributionBBoxes();
}
},
onRemove: function onRemove(map) {
L.TileLayer.prototype.onRemove.call(this, map);
if (this._map.attributionControl) {
this._map.attributionControl.removeAttribution(this._attributionText);
}
this._map.off('moveend zoomend resetview', this._findCopyrightBBox, this);
},
_fetchAttributionBBoxes: function _onMapMove() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = L.bind(function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
this._parseAttributionBBoxes(JSON.parse(xmlhttp.responseText));
}
}, this);
xmlhttp.open("GET", this._attributionUrl, true);
xmlhttp.send();
},
_parseAttributionBBoxes: function _parseAttributionBBoxes(json) {
if (!this._map) { return; }
var providers = json.copyrights.in;
for (var i = 0; i < providers.length; i++) {
if (providers[i].boundingBoxes) {
for (var j = 0; j < providers[i].boundingBoxes.length; j++) {
var box = providers[i].boundingBoxes[j];
providers[i].boundingBoxes[j] = L.latLngBounds([[box.east, box.north], [box.west, box.south]]);
}
}
}
this._map.on('moveend zoomend resetview', this._findCopyrightBBox, this);
this._attributionProviders = providers;
this._findCopyrightBBox();
},
_findCopyrightBBox: function _findCopyrightBBox() {
if (!this._map) { return; }
var providers = this._attributionProviders;
var visibleProviders = [];
var zoom = this._map.getZoom();
var visibleBounds = this._map.getBounds();
for (var i = 0; i < providers.length; i++) {
if (providers[i].minLevel < zoom && providers[i].maxLevel > zoom)
if (!providers[i].boundingBoxes) {
// No boxes = attribution always visible
visibleProviders.push(providers[i]);
break;
}
for (var j = 0; j < providers[i].boundingBoxes.length; j++) {
var box = providers[i].boundingBoxes[j];
if (visibleBounds.overlaps(box)) {
visibleProviders.push(providers[i]);
break;
}
}
}
var attributionText = '© <a href="https://legal.here.com/terms/serviceterms/gb/">HERE maps</a>';
if (attributionText !== this._attributionText && this._map.attributionControl) {
this._map.attributionControl.removeAttribution(this._attributionText);
this._map.attributionControl.addAttribution(this._attributionText = attributionText);
}
}
});
L.tileLayer.here = function (opts) {
return new L.TileLayer.HERE(opts);
}