Completed
Push — master ( b833ac...438a8c )
by Yannick
33:53
created

leaflet-Here.js ➔ initialize   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 16
dl 0
loc 47
rs 5.2941
c 0
b 0
f 0
nop 1
1
2
// 🍂class TileLayer.HERE
3
// Tile layer for HERE maps tiles.
4
L.TileLayer.HERE = L.TileLayer.extend({
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
5
6
	options: {
7
		subdomains: '1234',
8
		minZoom: 2,
9
		maxZoom: 18,
10
11
		// 🍂option scheme: String = 'normal.day'
12
		// The "map scheme", as documented in the HERE API.
13
		scheme: 'normal.day',
14
15
		// 🍂option resource: String = 'maptile'
16
		// The "map resource", as documented in the HERE API.
17
		resource: 'maptile',
18
19
		// 🍂option mapId: String = 'newest'
20
		// Version of the map tiles to be used, or a hash of an unique map
21
		mapId: 'newest',
22
23
		// 🍂option format: String = 'png8'
24
		// Image format to be used (`png8`, `png`, or `jpg`)
25
		format: 'png8',
26
27
		// 🍂option appId: String = ''
28
		// Required option. The `app_id` provided as part of the HERE credentials
29
		appId: '',
30
31
		// 🍂option appCode: String = ''
32
		// Required option. The `app_code` provided as part of the HERE credentials
33
		appCode: '',
34
35
		// 🍂option useCIT: boolean = false
36
		// Whether to use the CIT when loading the here-maptiles
37
		useCIT: false,
38
39
		// 🍂option useHTTPS: boolean = true
40
		// Whether to use HTTPS when loading the here-maptiles
41
		useHTTPS: true,
42
43
		// 🍂option language: String = ''
44
		// The language of the descriptions on the maps that are loaded
45
		language: '',
46
47
		// 🍂option language: String = ''
48
		// The second language of the descriptions on the maps that are loaded
49
		language2: '',
50
	},
51
52
53
	initialize: function initialize(options) {
54
		options = L.setOptions(this, options);
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
55
56
		// Decide if this scheme uses the aerial servers or the basemap servers
57
		var schemeStart = options.scheme.split('.')[0];
58
		options.tileResolution = 256;
59
60
		// {Base URL}{Path}/{resource (tile type)}/{map id}/{scheme}/{zoom}/{column}/{row}/{size}/{format}
61
		// ?app_id={YOUR_APP_ID}
62
		// &app_code={YOUR_APP_CODE}
63
		// &{param}={value}
64
65
		var params = [
66
			'app_id=' + encodeURIComponent(options.appId),
67
			'app_code=' + encodeURIComponent(options.appCode)
68
		];
69
		if(options.language) {
70
			params.push('lg=' + encodeURIComponent(options.language));
71
		}
72
		if(options.language2) {
73
			params.push('lg2=' + encodeURIComponent(options.language2));
74
		}
75
		var urlQuery = '?' + params.join('&');
76
77
		var path = '/maptile/2.1/{resource}/{mapId}/{scheme}/{z}/{x}/{y}/{tileResolution}/{format}' + urlQuery;
78
		var attributionPath = '/maptile/2.1/copyright/{mapId}?app_id={appId}&app_code={appCode}';
79
80
		// make sure the CIT-url can be used
81
		var baseUrl = 'maps' + (options.useCIT ? '.cit' : '') + '.api.here.com';
82
		var tileServer = 'base.' + baseUrl;
83
		if (schemeStart == 'satellite' || schemeStart == 'terrain' || schemeStart == 'hybrid') {
84
			tileServer = 'aerial.' + baseUrl;
85
		}
86
		if (options.scheme.indexOf('.traffic.') !== -1) {
87
			tileServer = 'traffic' + baseUrl;
88
		}
89
90
		var protocol = 'http' + (options.useHTTPS ? 's' : '');
91
		var tileUrl = protocol + '://{s}.' + tileServer + path;
92
93
		this._attributionUrl = L.Util.template(protocol + '://1.' + tileServer + attributionPath, this.options);
94
95
		L.TileLayer.prototype.initialize.call(this, tileUrl, options);
96
97
		this._attributionText = '';
98
99
	},
100
101
	onAdd: function onAdd(map) {
102
		L.TileLayer.prototype.onAdd.call(this, map);
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
103
104
		if (!this._attributionBBoxes) {
105
			this._fetchAttributionBBoxes();
106
		}
107
	},
108
109
	onRemove: function onRemove(map) {
110
		//
111
		// Remove the attribution text, and clear the cached text so it will be recalculated
112
		// if/when we are shown again.
113
		//
114
		this._map.attributionControl.removeAttribution(this._attributionText);
115
		this._attributionText = '';
116
117
		this._map.off('moveend zoomend resetview', this._findCopyrightBBox, this);
118
119
		//
120
		// Call the prototype last, once we've tidied up our own changes
121
		//
122
		L.TileLayer.prototype.onRemove.call(this, map);
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
123
	},
124
125
	_fetchAttributionBBoxes: function _onMapMove() {
126
		var xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
127
		xmlhttp.onreadystatechange = L.bind(function(){
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
128
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
129
				this._parseAttributionBBoxes(JSON.parse(xmlhttp.responseText));
130
			}
131
		}, this);
132
		xmlhttp.open("GET", this._attributionUrl, true);
133
		xmlhttp.send();
134
	},
135
136
	_parseAttributionBBoxes: function _parseAttributionBBoxes(json) {
137
		if (!this._map) { return; }
138
		var providers = json[this.options.scheme.split('.')[0]] || json.normal;
139
		for (var i=0; i<providers.length; i++) {
140
			if (providers[i].boxes) {
141
				for (var j=0; j<providers[i].boxes.length; j++) {
142
					var box = providers[i].boxes[j];
143
					providers[i].boxes[j] = L.latLngBounds( [ [box[0], box[1]], [box[2], box[3]] ]);
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
144
				}
145
			}
146
		}
147
148
		this._map.on('moveend zoomend resetview', this._findCopyrightBBox, this);
149
150
		this._attributionProviders = providers;
151
152
		this._findCopyrightBBox();
153
	},
154
155
	_findCopyrightBBox: function _findCopyrightBBox() {
156
		if (!this._map) { return; }
157
		var providers = this._attributionProviders;
158
		var visibleProviders = [];
159
		var zoom = this._map.getZoom();
160
		var visibleBounds = this._map.getBounds();
161
162
		for (var i=0; i<providers.length; i++) {
163
			if (providers[i].minLevel <= zoom && providers[i].maxLevel >= zoom) {
164
165
				if (!providers[i].boxes) {
166
					// No boxes = attribution always visible
167
					visibleProviders.push(providers[i]);
168
				} else {
169
					for (var j=0; j<providers[i].boxes.length; j++) {
170
						var box = providers[i].boxes[j];
171
						if (visibleBounds.intersects(box)) {
172
							visibleProviders.push(providers[i]);
173
							break;
174
						}
175
					}
176
				}
177
			}
178
		}
179
180
		var attributions = ['<a href="https://legal.here.com/en-gb/terms" target="_blank" rel="noopener noreferrer">HERE maps</a>'];
181
		for (var i=0; i<visibleProviders.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 162. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
182
			var provider = visibleProviders[i];
183
			attributions.push('<abbr title="' + provider.alt + '">' + provider.label + '</abbr>');
184
		}
185
186
		var attributionText = '© ' + attributions.join(', ') + '. ';
187
188
		if (attributionText !== this._attributionText) {
189
			this._map.attributionControl.removeAttribution(this._attributionText);
190
			this._map.attributionControl.addAttribution(this._attributionText = attributionText);
191
		}
192
	},
193
194
});
195
196
197
L.tileLayer.here = function(opts){
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
198
	return new L.TileLayer.HERE(opts);
0 ignored issues
show
Bug introduced by
The variable L seems to be never declared. If this is a global, consider adding a /** global: L */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
199
}
200