@maptrip/map-tiles 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -68,6 +68,7 @@ L.map("map", {
68
68
 
69
69
  - `token`: A valid and non-expired token for your account, see section _Authorization_ (**required**)
70
70
  - `attribution`: The copyright notes to show in the map
71
+ - `releaseDate`: Boolean value stating whether to append the map release data to the attribution; default is false.
71
72
  - `minZoom`: The minimum zoom level for the map (must not be smaller than 2)
72
73
  - `maxZoom`: The maximum zoom level for the map (must not be larger than 18)
73
74
 
@@ -108,6 +109,8 @@ new Map({
108
109
  `MapTripOpenLayersSource` can be configured with all properties listed in the section _Properties_, and also with these:
109
110
 
110
111
  - `token`: A valid and non-expired token for your account, see section _Authorization_ (**required**)
112
+ - `attribution`: The copyright notes to show in the map
113
+ - `releaseDate`: Boolean value stating whether to append the map release data to the attribution; default is false.
111
114
 
112
115
  ### Google Maps
113
116
 
@@ -274,6 +277,7 @@ To use them, you mapping API has to be able to use both base layers and overlays
274
277
 
275
278
  ## History
276
279
 
280
+ - 1.3.0: Added map release data for Leaflet and OpenLayers map attributions
277
281
  - 1.2.1: Fixed a bug in MapTripLeafletGridLayer type implementation
278
282
  - 1.2.0: Added TypeScript declaration file
279
283
  - 1.1.1: Added rendering config for foot_path layer
@@ -1,4 +1,5 @@
1
1
  import { PointXY, RENDERING_CONFIG } from '../../types/interfaces.js';
2
+ import { MapReleaseFetcher } from '../../util/MapReleaseFetcher.js';
2
3
  import { VectorTileFactory } from '../../core/VectorTileFactory.js';
3
4
  import { default as L } from 'leaflet';
4
5
 
@@ -7,6 +8,11 @@ declare class MapTripLeafletGridLayer extends L.GridLayer {
7
8
  MAX_ZOOM: number;
8
9
  token: string;
9
10
  factory: VectorTileFactory;
11
+ releaseFetcher: MapReleaseFetcher;
12
+ provider: string;
13
+ attribution: string;
14
+ release: string;
15
+ releaseDate: boolean;
10
16
  constructor(options: {
11
17
  provider?: string;
12
18
  baseMap?: boolean;
@@ -19,7 +25,12 @@ declare class MapTripLeafletGridLayer extends L.GridLayer {
19
25
  attribution?: string;
20
26
  minZoom?: number;
21
27
  maxZoom?: number;
28
+ releaseDate?: boolean;
22
29
  });
23
30
  createTile(coords: PointXY): HTMLCanvasElement;
31
+ private initializeAttribution;
32
+ private setRelease;
33
+ private setAttribution;
34
+ private onBaseLayerChange;
24
35
  }
25
36
  export { MapTripLeafletGridLayer };
@@ -1,3 +1,4 @@
1
+ import { MapReleaseFetcher } from '../../main.js';
1
2
  import { RENDERING_CONFIG } from '../../types/interfaces.js';
2
3
  import { VectorTileFactory } from '../../core/VectorTileFactory.js';
3
4
  import { default as XYZ } from 'ol/source/XYZ.js';
@@ -5,6 +6,9 @@ import { default as XYZ } from 'ol/source/XYZ.js';
5
6
  declare class MapTripOpenLayersSource extends XYZ {
6
7
  token: string;
7
8
  factory: VectorTileFactory;
9
+ release: string;
10
+ releaseFetcher: MapReleaseFetcher;
11
+ releaseDate: boolean;
8
12
  constructor(properties: {
9
13
  provider?: string;
10
14
  baseMap?: boolean;
@@ -14,6 +18,9 @@ declare class MapTripOpenLayersSource extends XYZ {
14
18
  detourProfile?: string;
15
19
  renderingConfig?: RENDERING_CONFIG;
16
20
  token?: string;
21
+ releaseDate?: boolean;
17
22
  });
23
+ private fetchAndSetAttribution;
24
+ private updateAttribution;
18
25
  }
19
26
  export { MapTripOpenLayersSource };
@@ -1,9 +1,10 @@
1
1
  import { MapTripOpenLayersSource } from './api-extensions/openlayers/MapTripOpenLayersSource.js';
2
2
  import { MapTripLeafletGridLayer } from './api-extensions/leaflet/MapTripLeafletGridLayer.js';
3
3
  import { MapTripGoogleMapType } from './api-extensions/google/MapTripGoogleMapType.js';
4
+ import { MapReleaseFetcher } from './util/MapReleaseFetcher.js';
4
5
  import { RENDERING_CONFIG_TRAFFIC } from './core/rendering_config_traffic.js';
5
6
  import { RENDERING_CONFIG_GRAYSCALE } from './core/rendering_config_grayscale.js';
6
7
  import { RENDERING_CONFIG_DEFAULT } from './core/rendering_config_default.js';
7
8
  import { VectorTileFactory } from './core/VectorTileFactory.js';
8
9
 
9
- export { VectorTileFactory, RENDERING_CONFIG_DEFAULT, RENDERING_CONFIG_GRAYSCALE, RENDERING_CONFIG_TRAFFIC, MapTripLeafletGridLayer, MapTripGoogleMapType, MapTripOpenLayersSource, };
10
+ export { VectorTileFactory, RENDERING_CONFIG_DEFAULT, RENDERING_CONFIG_GRAYSCALE, RENDERING_CONFIG_TRAFFIC, MapReleaseFetcher, MapTripLeafletGridLayer, MapTripGoogleMapType, MapTripOpenLayersSource, };
package/dist/map-tiles.js CHANGED
@@ -3248,7 +3248,7 @@ class VectorTileFactory {
3248
3248
  throw new Error("Unknown provider");
3249
3249
  }
3250
3250
  if (this._properties.traffic && !PROVIDER_TRAFFIC[this._properties.provider]) {
3251
- throw new Error("There is now traffic information for this provider");
3251
+ throw new Error("There is no traffic information for this provider");
3252
3252
  }
3253
3253
  if (!this._properties.baseMap && !this._properties.traffic) {
3254
3254
  throw new Error(
@@ -4548,6 +4548,81 @@ const RENDERING_CONFIG_GRAYSCALE = {
4548
4548
  }
4549
4549
  };
4550
4550
 
4551
+ class MapReleaseFetcher {
4552
+ _baseUrl;
4553
+ _token;
4554
+ _tomtom = null;
4555
+ _osm = null;
4556
+ _here = null;
4557
+ constructor(token) {
4558
+ this._baseUrl = "https://v1.maptrip.de/?CMD=GETFILE&PROJECT=${project}&RELATIVEPATH=/data/mapset/data/routing_geocoder/admin.bin&acao=true";
4559
+ this._token = token;
4560
+ }
4561
+ updateUrl(provider) {
4562
+ if (provider === "tomtom") {
4563
+ this._baseUrl = this._baseUrl.replace("${project}", "TomTom_latest");
4564
+ } else if (provider === "openstreetmap") {
4565
+ this._baseUrl = this._baseUrl.replace("${project}", "OSM_latest");
4566
+ } else {
4567
+ this._baseUrl = this._baseUrl.replace("${project}", "HERE_latest");
4568
+ }
4569
+ }
4570
+ async getRelease(provider) {
4571
+ const providerLowercase = provider.toLowerCase();
4572
+ if (providerLowercase === "tomtom") {
4573
+ if (!this._tomtom) {
4574
+ this._tomtom = this.fetchAndExtractRelease(providerLowercase);
4575
+ }
4576
+ return this._tomtom;
4577
+ } else if (providerLowercase === "here") {
4578
+ if (!this._here) {
4579
+ this._here = this.fetchAndExtractRelease(providerLowercase);
4580
+ }
4581
+ return this._here;
4582
+ } else {
4583
+ if (!this._osm) {
4584
+ this._osm = this.fetchAndExtractRelease(providerLowercase);
4585
+ }
4586
+ return this._osm;
4587
+ }
4588
+ }
4589
+ async fetchAndExtractRelease(provider) {
4590
+ this.updateUrl(provider);
4591
+ return new Promise((resolve, reject) => {
4592
+ var xhr = new XMLHttpRequest();
4593
+ xhr.onreadystatechange = () => {
4594
+ if (xhr.readyState !== XMLHttpRequest.DONE)
4595
+ return;
4596
+ if (xhr.status === 200) {
4597
+ const text = new TextDecoder("latin1").decode(xhr.response);
4598
+ const textString = this.extractTargetString(text);
4599
+ const release = ` ${textString}`;
4600
+ if (provider === "tomtom") {
4601
+ this._tomtom = Promise.resolve(release);
4602
+ } else if (provider === "here") {
4603
+ this._here = Promise.resolve(release);
4604
+ } else if (provider === "openstreetmap") {
4605
+ this._osm = Promise.resolve(release);
4606
+ }
4607
+ resolve(release);
4608
+ } else {
4609
+ reject(`Error fetching: ${xhr.status}`);
4610
+ }
4611
+ };
4612
+ xhr.open("GET", this._baseUrl, true);
4613
+ xhr.setRequestHeader("Authorization", `Bearer ${this._token}`);
4614
+ xhr.setRequestHeader("Accept", "*/*");
4615
+ xhr.setRequestHeader("Content-Type", "application/octet-stream");
4616
+ xhr.responseType = "arraybuffer";
4617
+ xhr.send(null);
4618
+ });
4619
+ }
4620
+ extractTargetString(text) {
4621
+ const match = text.match(/2\d{3}Q[1-4]/);
4622
+ return match ? match[0] : null;
4623
+ }
4624
+ }
4625
+
4551
4626
  class MapTripGoogleMapType {
4552
4627
  tileSize = new google.maps.Size(256, 256);
4553
4628
  alt = null;
@@ -19092,10 +19167,31 @@ class MapTripLeafletGridLayer extends L$1.GridLayer {
19092
19167
  MAX_ZOOM = MAX_ZOOM;
19093
19168
  token;
19094
19169
  factory;
19170
+ releaseFetcher;
19171
+ provider;
19172
+ attribution;
19173
+ release;
19174
+ releaseDate;
19095
19175
  constructor(options) {
19096
19176
  super(options);
19097
19177
  this.token = options.token;
19178
+ this.provider = options.provider ? options.provider?.toLowerCase() : "openstreetmap";
19098
19179
  this.options.attribution = options.attribution ? options.attribution : ATTRIBUTION;
19180
+ this.releaseFetcher = new MapReleaseFetcher(this.token);
19181
+ this.release = "";
19182
+ this.attribution = "";
19183
+ this.releaseDate = options.releaseDate ? options.releaseDate : false;
19184
+ if (this.releaseDate) {
19185
+ this.initializeAttribution();
19186
+ this.on("add", () => {
19187
+ if (this._map) {
19188
+ this._map.on(
19189
+ "baselayerchange",
19190
+ (event) => this.onBaseLayerChange(event)
19191
+ );
19192
+ }
19193
+ });
19194
+ }
19099
19195
  this.options.minZoom = options.minZoom ? Math.max(this.MIN_ZOOM, options.minZoom) : this.MIN_ZOOM;
19100
19196
  this.options.maxZoom = options.maxZoom ? Math.min(this.MAX_ZOOM, options.maxZoom) : this.MAX_ZOOM;
19101
19197
  this.factory = VectorTileFactory.createFactory(options);
@@ -19103,6 +19199,57 @@ class MapTripLeafletGridLayer extends L$1.GridLayer {
19103
19199
  createTile(coords) {
19104
19200
  return this.factory.getJsonTile(coords.z, coords.x, coords.y, this.token);
19105
19201
  }
19202
+ async initializeAttribution() {
19203
+ await this.setRelease();
19204
+ await this.setAttribution();
19205
+ }
19206
+ // Set an original release
19207
+ async setRelease() {
19208
+ const release = await this.releaseFetcher.getRelease(this.provider);
19209
+ if (release) {
19210
+ this.release = release;
19211
+ }
19212
+ }
19213
+ async setAttribution() {
19214
+ if (!this._map)
19215
+ return;
19216
+ try {
19217
+ const newRelease = await this.releaseFetcher.getRelease(this.provider);
19218
+ if (newRelease && newRelease !== this.release) {
19219
+ this.release = newRelease;
19220
+ }
19221
+ this._map.attributionControl.removeAttribution(this.options.attribution);
19222
+ this.attribution = `${this.options.attribution} ${this.release}`;
19223
+ this._map.attributionControl.addAttribution(this.attribution);
19224
+ const map = this._map;
19225
+ this._map.addEventListener("baselayerchange", () => {
19226
+ map.attributionControl.removeAttribution(this.attribution);
19227
+ });
19228
+ } catch (error) {
19229
+ console.error("Error fetching release version: ", error);
19230
+ }
19231
+ }
19232
+ async onBaseLayerChange(event) {
19233
+ if (!this._map) {
19234
+ return;
19235
+ }
19236
+ this._map.attributionControl.removeAttribution(this.attribution);
19237
+ const eventProvider = event.layer.options.provider;
19238
+ let newProvider;
19239
+ if (eventProvider) {
19240
+ if (eventProvider.toLowerCase() === "opensreetmap") {
19241
+ newProvider = "openstreetmap";
19242
+ } else if (eventProvider.toLowerCase() === "tomtom") {
19243
+ newProvider = "tomtom";
19244
+ } else if (eventProvider.toLowerCase() === "here") {
19245
+ newProvider = "here";
19246
+ } else {
19247
+ newProvider = "openstreetmap";
19248
+ }
19249
+ this.provider = newProvider;
19250
+ await this.setAttribution();
19251
+ }
19252
+ }
19106
19253
  }
19107
19254
 
19108
19255
  /**
@@ -26999,8 +27146,10 @@ const XYZ$1 = XYZ;
26999
27146
  class MapTripOpenLayersSource extends XYZ$1 {
27000
27147
  token;
27001
27148
  factory;
27149
+ release = "";
27150
+ releaseFetcher;
27151
+ releaseDate;
27002
27152
  constructor(properties) {
27003
- super();
27004
27153
  const options = {};
27005
27154
  const projection = "EPSG:3857";
27006
27155
  const tileGrid = createXYZ({
@@ -27034,8 +27183,36 @@ class MapTripOpenLayersSource extends XYZ$1 {
27034
27183
  });
27035
27184
  this.token = properties.token;
27036
27185
  this.factory = VectorTileFactory.createFactory(properties);
27186
+ this.releaseFetcher = new MapReleaseFetcher(this.token);
27187
+ this.releaseDate = properties.releaseDate ? properties.releaseDate : false;
27188
+ if (this.releaseDate) {
27189
+ this.fetchAndSetAttribution(properties.provider, properties.token);
27190
+ this.on("change", () => this.updateAttribution(properties.provider));
27191
+ }
27192
+ }
27193
+ async fetchAndSetAttribution(provider, token) {
27194
+ try {
27195
+ const release = await this.releaseFetcher.getRelease(provider);
27196
+ if (release) {
27197
+ this.release = release;
27198
+ this.setAttributions(`${ATTRIBUTION} ${this.release}`);
27199
+ }
27200
+ } catch (error) {
27201
+ console.error("Error fetching release version: ", error);
27202
+ }
27203
+ }
27204
+ async updateAttribution(provider) {
27205
+ try {
27206
+ const release = await this.releaseFetcher.getRelease(provider);
27207
+ if (release && release !== this.release) {
27208
+ this.release = release;
27209
+ this.setAttributions(`${ATTRIBUTION} ${this.release}`);
27210
+ }
27211
+ } catch (error) {
27212
+ console.error("Error updating attribution: ", error);
27213
+ }
27037
27214
  }
27038
27215
  }
27039
27216
 
27040
- export { MapTripGoogleMapType, MapTripLeafletGridLayer, MapTripOpenLayersSource, RENDERING_CONFIG_DEFAULT, RENDERING_CONFIG_GRAYSCALE, RENDERING_CONFIG_TRAFFIC, VectorTileFactory };
27217
+ export { MapReleaseFetcher, MapTripGoogleMapType, MapTripLeafletGridLayer, MapTripOpenLayersSource, RENDERING_CONFIG_DEFAULT, RENDERING_CONFIG_GRAYSCALE, RENDERING_CONFIG_TRAFFIC, VectorTileFactory };
27041
27218
  //# sourceMappingURL=map-tiles.js.map