basemap.js

  1. 'use strict';
  2. /**
  3. *
  4. * The `Basemap` module provides basemap related functions.
  5. *
  6. * This module exports an object with the following properties
  7. * - `Basemap` esri/dijit/Basemap class
  8. * - `BasemapGallery` esri/dijit/BasemapGallery class
  9. * - `BasemapLayer` esri/dijit/BasemapLayer class
  10. * - `makeBasemaps` function that makes a basemap gallery based on the settings provided
  11. */
  12. // Basemap related modules
  13. module.exports = function (esriBundle) {
  14. /**
  15. * Make basemap gallery based on the settings of basemap metadata.
  16. *
  17. * @function makeBasemaps
  18. * @param {Object} basemapsConfig json config object contains collection/array of basemap settings
  19. * @param {esriMap} map ESRI map object
  20. * @param {String} anchorId DOM element where the dijit will be placed
  21. * @return {Object} an object with the following properties:
  22. * <ul>
  23. * <li>setBasemap {function} set current basemap with a basemap uid</li>
  24. * <li>basemapGallery {object} basemapGallery object</li>
  25. * </ul>
  26. */
  27. function makeBasemaps(basemapsConfig, map, anchorId) {
  28. let basemap;
  29. let basemapGallery = new esriBundle.BasemapGallery({
  30. showArcGISBasemaps: false,
  31. map: map
  32. }, anchorId);
  33. // iterate throuh basemap configs
  34. basemapsConfig.forEach(basemapConfig => {
  35. let layers = [];
  36. basemapConfig.layers.forEach(layerConfig => {
  37. // create basemap, add to basemap gallery
  38. let layer = new esriBundle.BasemapLayer({
  39. url: layerConfig.url
  40. });
  41. layers.push(layer);
  42. });
  43. basemap = new esriBundle.Basemap({
  44. id: basemapConfig.id,
  45. layers: layers,
  46. title: basemapConfig.name,
  47. thumbnailUrl: basemapConfig.thumbnailUrl,
  48. wkid: basemapConfig.wkid
  49. });
  50. basemapGallery.add(basemap);
  51. });
  52. // finalize basmap gallery
  53. basemapGallery.startup();
  54. // display message
  55. // TODO: add ui hook? to display msg on screen
  56. basemapGallery.on('error', msg => {
  57. console.error('basemap gallery error:', msg);
  58. });
  59. // Set basemap by id
  60. function setBasemap(id) {
  61. // set the basemap based on the id provided
  62. basemapGallery.select(id);
  63. }
  64. return {
  65. setBasemap: setBasemap,
  66. basemapGallery: basemapGallery
  67. };
  68. }
  69. return {
  70. Basemap: esriBundle.Basemap,
  71. BasemapGallery: esriBundle.BasemapGallery,
  72. BasemapLayer: esriBundle.BasemapLayer,
  73. makeBasemaps: makeBasemaps
  74. };
  75. };