Skip to content

Commit

Permalink
Fix handling of ion and Bing token
Browse files Browse the repository at this point in the history
  • Loading branch information
Son-HNguyen committed Aug 7, 2020
1 parent 30d4f59 commit 52aa4f9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
76 changes: 44 additions & 32 deletions 3dwebclient/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var urlController = new UrlController();
/*--------------------------------- set globe variables ----------------------------------------*/
// BingMapsAPI Key for Bing Imagery Layers and Geocoder
// If this is not valid, the Bing Imagery Layers will be removed and the Bing Geocoder will be replaced with OSM Nominatim
var bingToken = CitydbUtil.parse_query_string('bingToken', window.location.href);
var bingToken = urlController.getUrlParaValue('bingToken', window.location.href, CitydbUtil);
if (Cesium.defined(bingToken) && bingToken !== "") {
Cesium.BingMapsApi.defaultKey = bingToken;
}
Expand Down Expand Up @@ -313,44 +313,51 @@ function observeActiveLayer() {
}

function adjustIonFeatures() {
// If neither BingMapsAPI key nor ion access token is present, remove Bing Maps from the Imagery Providers
if (!Cesium.defined(Cesium.BingMapsApi.defaultKey) || Cesium.BingMapsApi.defaultKey === "") {
var imageryProviders = cesiumViewer.baseLayerPicker.viewModel.imageryProviderViewModels;
var i = 0;
while (i < imageryProviders.length) {
if (imageryProviders[i].name.indexOf("Bing Maps") !== -1) {
//imageryProviders[i]._creationCommand.canExecute = false;
imageryProviders.remove(imageryProviders[i]);
// If ion token is not available, remove Cesium World Terrain from the Terrain Providers
if (!Cesium.defined(ionToken) || ionToken === "") {
var terrainProviders = cesiumViewer.baseLayerPicker.viewModel.terrainProviderViewModels;
i = 0;
while (i < terrainProviders.length) {
if (terrainProviders[i].name.indexOf("Cesium World Terrain") !== -1) {
//terrainProviders[i]._creationCommand.canExecute = false;
terrainProviders.remove(terrainProviders[i]);
} else {
i++;
}
}
console.warn("Please enter your Bing Maps API token using the URL-parameter \"bingToken=<your-token>\" and refresh the page if you wish to use Bing Maps.");

// Set default imagery to ESRI World Imagery
cesiumViewer.baseLayerPicker.viewModel.selectedImagery = imageryProviders[3];
// Set default imagery to an open-source terrain
cesiumViewer.baseLayerPicker.viewModel.selectedTerrain = terrainProviders[0];
console.warn("Due to invalid or missing ion access token from user, Cesium World Terrain has been removed. Please enter your ion access token using the URL-parameter \"ionToken=<your-token>\" and refresh the page if you wish to use ion features.");

// Disable auto-complete of OSM Geocoder due to OSM usage limitations
// see https://operations.osmfoundation.org/policies/nominatim/#unacceptable-use
cesiumViewer._geocoder._viewModel.autoComplete = false;
}
// Cesium ion uses Bing Maps by default -> no need to insert Bing token if an ion token is already available

// If neither BingMapsAPI key nor ion access token is present, remove Bing Maps from the Imagery Providers
if (!Cesium.defined(Cesium.BingMapsApi.defaultKey) || Cesium.BingMapsApi.defaultKey === "") {
var imageryProviders = cesiumViewer.baseLayerPicker.viewModel.imageryProviderViewModels;
var i = 0;
while (i < imageryProviders.length) {
if (imageryProviders[i].name.indexOf("Bing Maps") !== -1) {
//imageryProviders[i]._creationCommand.canExecute = false;
imageryProviders.remove(imageryProviders[i]);
} else {
i++;
}
}

// Set default imagery to ESRI World Imagery
cesiumViewer.baseLayerPicker.viewModel.selectedImagery = imageryProviders[3];

// Remove Cesium World Terrain from the Terrain Providers
// var terrainProviders = cesiumViewer.baseLayerPicker.viewModel.terrainProviderViewModels;
// i = 0;
// while (i < terrainProviders.length) {
// if (terrainProviders[i].name.indexOf("Cesium World Terrain") !== -1) {
// //terrainProviders[i]._creationCommand.canExecute = false;
// terrainProviders.remove(terrainProviders[i]);
// } else {
// i++;
// }
// }
// console.log("Due to invalid or missing ion access token from user, Cesium World Terrain has been removed.");

// Set default imagery to an open-source terrain
// cesiumViewer.baseLayerPicker.viewModel.selectedTerrain = terrainProviders[0];
console.warn("Please enter your ion access token using the URL-parameter \"ionToken=<your-token>\" and refresh the page if you wish to use ion features.");
// Disable auto-complete of OSM Geocoder due to OSM usage limitations
// see https://operations.osmfoundation.org/policies/nominatim/#unacceptable-use
cesiumViewer._geocoder._viewModel.autoComplete = false;

console.warn("Due to invalid or missing Bing access token from user, all Bing Maps have been removed. Please enter your Bing Maps API token using the URL-parameter \"bingToken=<your-token>\" and refresh the page if you wish to use Bing Maps.");
} else {
console.error("A Bing token has been detected. This requires an ion token to display the terrain correctly. Please either remove the Bing token in the URL to use the default terrain and imagery, or insert an ion token in addition to the existing Bing token to use Cesium World Terrain and Bing Maps.")
CitydbUtil.showAlertWindow("OK", "Error loading terrain", "A Bing token has been detected. This requires an ion token to display the terrain correctly. Please either remove the Bing token in the URL to use the default terrain and imagery, or insert an ion token in addition to the existing Bing token to use Cesium World Terrain and Bing Maps. Please refer to <a href='https://github.com/3dcitydb/3dcitydb-web-map/releases/tag/v1.9.0' target='_blank'>https://github.com/3dcitydb/3dcitydb-web-map/releases/tag/v1.9.0</a> for more information.");
}
}
}

/*--------------------------------- methods and functions ----------------------------------------*/
Expand Down Expand Up @@ -725,11 +732,16 @@ function flyToCameraPosition(cameraPosition) {

// Creation of a scene link for sharing with other people..
function showSceneLink() {
var tokens = {
ionToken: ionToken,
bingToken: bingToken
}
var sceneLink = urlController.generateLink(
webMap,
addWmsViewModel,
addTerrainViewModel,
addSplashWindowModel,
tokens,
signInController,
googleClientId,
splashController,
Expand Down
18 changes: 13 additions & 5 deletions 3dwebclient/utils/UrlController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var UrlController = /** @class */ (function () {
// "url": "u",
"showOnStart": "ss",
"ionToken": "it",
"bingToken": "bt",
"debug": "db",
"googleClientId": "gid"
};
Expand Down Expand Up @@ -85,14 +86,14 @@ var UrlController = /** @class */ (function () {
// return the value defined by the URL parameter
UrlController.prototype.getUrlParaValue = function (parameter, url, CitydbUtil) {
var result = CitydbUtil.parse_query_string(this.getUrlParaForward(parameter), url);
if (typeof result === "undefined" || result === "") {
if (result == null || result === "") {
// reverse mapping
// result = CitydbUtil.parse_query_string(this.getUrlParaAuxReverse(parameter), url);
result = CitydbUtil.parse_query_string(parameter, url);
}
return result;
};
UrlController.prototype.generateLink = function (webMap, addWmsViewModel, addTerrainViewModel, addSplashWindowModel, signInController, googleClientId, splashController, cesiumViewer, Cesium) {
UrlController.prototype.generateLink = function (webMap, addWmsViewModel, addTerrainViewModel, addSplashWindowModel, tokens, signInController, googleClientId, splashController, cesiumViewer, Cesium) {
var cameraPosition = this.getCurrentCameraPostion(cesiumViewer, Cesium);
var projectLink = location.protocol + '//' + location.host + location.pathname + '?';
var clock = cesiumViewer.cesiumWidget.clock;
Expand All @@ -114,17 +115,24 @@ var UrlController = /** @class */ (function () {
'&' + this.getUrlParaForward('roll') + '=' + Math.round(cameraPosition.roll * 1e2) / 1e2 +
'&' + this.layersToQuery(webMap, Cesium);
var basemap = this.basemapToQuery(addWmsViewModel, cesiumViewer, Cesium);
if (basemap != null) {
if (basemap != null && basemap !== "") {
projectLink = projectLink + '&' + basemap;
}
var terrain = this.terrainToQuery(addTerrainViewModel, cesiumViewer, Cesium);
if (terrain != null) {
if (terrain != null && terrain !== "") {
projectLink = projectLink + '&' + terrain;
}
var splashWindow = this.splashWindowToQuery(addSplashWindowModel, splashController, Cesium);
if (splashWindow != null) {
if (splashWindow != null && splashWindow !== "") {
projectLink = projectLink + '&' + splashWindow;
}
// export ion and Bing token if available
if (tokens.ionToken != null && tokens.ionToken !== "") {
projectLink = projectLink + '&' + this.getUrlParaForward('ionToken') + '=' + tokens.ionToken;
}
if (tokens.bingToken != null && tokens.bingToken !== "") {
projectLink = projectLink + '&' + this.getUrlParaForward('bingToken') + '=' + tokens.bingToken;
}
// only export client ID if user is logged in
if ((signInController && signInController.clientID && signInController.isSignIn())) {
projectLink = projectLink + '&' + this.getUrlParaForward('googleClientId') + '=' + (signInController.clientID ? signInController.clientID : googleClientId);
Expand Down
18 changes: 14 additions & 4 deletions ts/UrlController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class UrlController {
"showOnStart": "ss",

"ionToken": "it",
"bingToken": "bt",
"debug": "db",
"googleClientId": "gid"
};
Expand Down Expand Up @@ -94,7 +95,7 @@ class UrlController {
// return the value defined by the URL parameter
public getUrlParaValue(parameter: string, url: string, CitydbUtil: any): string {
let result: any = CitydbUtil.parse_query_string(this.getUrlParaForward(parameter), url);
if (typeof result === "undefined" || result === "") {
if (result == null || result === "") {
// reverse mapping
// result = CitydbUtil.parse_query_string(this.getUrlParaAuxReverse(parameter), url);
result = CitydbUtil.parse_query_string(parameter, url);
Expand All @@ -107,6 +108,7 @@ class UrlController {
addWmsViewModel: any,
addTerrainViewModel: any,
addSplashWindowModel: any,
tokens: any,
signInController: any,
googleClientId: any,
splashController: SplashController,
Expand Down Expand Up @@ -136,20 +138,28 @@ class UrlController {
'&' + this.getUrlParaForward('roll') + '=' + Math.round(cameraPosition.roll * 1e2) / 1e2 +
'&' + this.layersToQuery(webMap, Cesium);
let basemap = this.basemapToQuery(addWmsViewModel, cesiumViewer, Cesium);
if (basemap != null) {
if (basemap != null && basemap !== "") {
projectLink = projectLink + '&' + basemap;
}

let terrain = this.terrainToQuery(addTerrainViewModel, cesiumViewer, Cesium);
if (terrain != null) {
if (terrain != null && terrain !== "") {
projectLink = projectLink + '&' + terrain;
}

let splashWindow = this.splashWindowToQuery(addSplashWindowModel, splashController, Cesium);
if (splashWindow != null) {
if (splashWindow != null && splashWindow !== "") {
projectLink = projectLink + '&' + splashWindow;
}

// export ion and Bing token if available
if (tokens.ionToken != null && tokens.ionToken !== "") {
projectLink = projectLink + '&' + this.getUrlParaForward('ionToken') + '=' + tokens.ionToken;
}
if (tokens.bingToken != null && tokens.bingToken !== "") {
projectLink = projectLink + '&' + this.getUrlParaForward('bingToken') + '=' + tokens.bingToken;
}

// only export client ID if user is logged in
if ((signInController && signInController.clientID && signInController.isSignIn())) {
projectLink = projectLink + '&' + this.getUrlParaForward('googleClientId') + '=' + (signInController.clientID ? signInController.clientID : googleClientId);
Expand Down

0 comments on commit 52aa4f9

Please sign in to comment.