Skip to content

Commit

Permalink
getMetaData async
Browse files Browse the repository at this point in the history
  • Loading branch information
tentone committed Dec 20, 2022
1 parent 5334a4e commit b4f9f9d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 67 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "geo-three",
"version": "0.1.9",
"version": "0.1.10",
"type": "module",
"description": "geo-three is library for tile based geographic map layers in with three.js supporting selective loading/unloading of real-time generated 3D tiles",
"main": "build/Main.d.ts",
Expand Down Expand Up @@ -38,7 +38,7 @@
"author": "Tentone",
"license": "MIT",
"peerDependencies": {
"three": "^0.120.0"
"three": ">0.120.0"
},
"devDependencies": {
"@rollup/plugin-strip": "~2.1.0",
Expand Down
15 changes: 8 additions & 7 deletions source/providers/BingMapsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export class BingMapsProvider extends MapProvider
*/
public subdomain: string = 't1';

/**
* Metadata of the provider.
*/
public meta: any = null;

/**
* @param apiKey - Bing API key.
* @param type - Type provider.
Expand Down Expand Up @@ -99,16 +104,12 @@ export class BingMapsProvider extends MapProvider
*
* http://ecn.\{subdomain\}.tiles.virtualearth.net/tiles/r\{quadkey\}.jpeg?g=129&mkt=\{culture\}&shading=hill&stl=H
*/
public getMetaData(): void
public async getMetaData(): Promise<void>
{
const address = BingMapsProvider.ADDRESS + '/REST/V1/Imagery/Metadata/RoadOnDemand?output=json&include=ImageryProviders&key=' + this.apiKey;
const data = await XHRUtils.get(address);

XHRUtils.get(address, function(data)
{
const meta = JSON.parse(data);

// TODO <FILL METADATA>
});
this.meta = JSON.parse(data);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions source/providers/MapBoxProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,18 @@ export class MapBoxProvider extends MapProvider
this.version = version;
}

public getMetaData(): void
public async getMetaData(): Promise<void>
{
const address = MapBoxProvider.ADDRESS + this.version + '/' + this.mapId + '.json?access_token=' + this.apiToken;

XHRUtils.get(address, (data: any): void =>
{
const meta = JSON.parse(data);

this.name = meta.name;
this.minZoom = meta.minZoom;
this.maxZoom = meta.maxZoom;
this.bounds = meta.bounds;
this.center = meta.center;
});
const data = await XHRUtils.get(address);

const meta = JSON.parse(data);
this.name = meta.name;
this.minZoom = meta.minZoom;
this.maxZoom = meta.maxZoom;
this.bounds = meta.bounds;
this.center = meta.center;
}

public fetchTile(zoom: number, x: number, y: number): Promise<any>
Expand Down
2 changes: 1 addition & 1 deletion source/providers/MapProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ export abstract class MapProvider
*
* Usually map server have API method to retrieve TileJSON metadata.
*/
public getMetaData(): void {}
public async getMetaData(): Promise<void> {}
}
20 changes: 9 additions & 11 deletions source/providers/OpenMapTilesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,18 @@ export class OpenMapTilesProvider extends MapProvider
this.theme = theme;
}

public getMetaData(): void
public async getMetaData(): Promise<void>
{
const address = this.address + 'styles/' + this.theme + '.json';

XHRUtils.get(address, (data: any) =>
{
const meta = JSON.parse(data);
this.name = meta.name;
this.format = meta.format;
this.minZoom = meta.minZoom;
this.maxZoom = meta.maxZoom;
this.bounds = meta.bounds;
this.center = meta.center;
});
const data = await XHRUtils.get(address);
const meta = JSON.parse(data);
this.name = meta.name;
this.format = meta.format;
this.minZoom = meta.minZoom;
this.maxZoom = meta.maxZoom;
this.bounds = meta.bounds;
this.center = meta.center;
}

public fetchTile(zoom: number, x: number, y: number): Promise<any>
Expand Down
54 changes: 19 additions & 35 deletions source/utils/XHRUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,21 @@ export class XHRUtils
* @param onLoad - On load callback.
* @param onError - On progress callback.
*/
public static get(url: string, onLoad?: Function, onError?: Function): XMLHttpRequest
public static async get(url: string): Promise<any>
{
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('text/plain');
xhr.open('GET', url, true);
return new Promise(function(resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.overrideMimeType('text/plain');
xhr.open('GET', url, true);

if (onLoad !== undefined)
{
xhr.onload = function()
{
onLoad(xhr.response);
resolve(xhr.response);
};
}

if (onError !== undefined)
{
// @ts-ignore
xhr.onerror = onError;
}

xhr.send(null);

return xhr;
xhr.onerror = reject;
xhr.send(null);
});
}

/**
Expand All @@ -42,29 +34,21 @@ export class XHRUtils
* @param onLoad - On load callback.
* @param onError - On progress callback.
*/
public static getRaw(url: string, onLoad?: Function, onError?: Function): XMLHttpRequest
public static async getRaw(url: string): Promise<ArrayBuffer>
{
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', url, true);
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', url, true);

if (onLoad !== undefined)
{
xhr.onload = function()
xhr.onload = function()
{
onLoad(xhr.response);
resolve(xhr.response);
};
}

if (onError !== undefined)
{
// @ts-ignore
xhr.onerror = onError;
}

xhr.send(null);

return xhr;
xhr.onerror = reject;
xhr.send(null);
});
}

/**
Expand Down

0 comments on commit b4f9f9d

Please sign in to comment.