map/basemap.js

  1. 'use strict';
  2. /**
  3. * Make basemap gallery based on the settings of basemap metadata.
  4. *
  5. * @function
  6. * @param {Object} esriBundle ESRI modules from the initial startup
  7. * @param {Array} basemapsConfig array of basemap settings in the form { id: string, layers: [string], title: string, thumbnailUrl: string, wkid: integer }
  8. * @param {esriMap} map ESRI map object
  9. * @return {Object} an object with the following properties:
  10. * <ul>
  11. * <li>setBasemap {function} set current basemap with a basemap uid</li>
  12. * <li>basemapGallery {object} basemapGallery object</li>
  13. * </ul>
  14. */
  15. function initBasemaps(esriBundle, basemapsConfig, map) {
  16. const basemapGallery = new esriBundle.BasemapGallery({ showArcGISBasemaps: false, map });
  17. // iterate throuh basemap configs
  18. basemapsConfig.forEach(basemapConfig => {
  19. // create basemap, add to basemap gallery
  20. const layers = basemapConfig.layers.map(config =>
  21. new esriBundle.BasemapLayer({ url: config.url, opacity: basemapConfig.opacity }));
  22. const basemap = new esriBundle.Basemap({
  23. id: basemapConfig.id,
  24. layers: layers,
  25. title: basemapConfig.name,
  26. thumbnailUrl: basemapConfig.thumbnailUrl,
  27. wkid: basemapConfig.wkid
  28. });
  29. basemapGallery.add(basemap);
  30. });
  31. // finalize basmap gallery
  32. basemapGallery.startup();
  33. // display message
  34. // TODO: add ui hook? to display msg on screen
  35. basemapGallery.on('error', msg => { throw new Error(msg); });
  36. return basemapGallery;
  37. }
  38. /**
  39. *
  40. * The `Basemap` module provides basemap related functions.
  41. *
  42. * This module exports an object with the following properties
  43. * - `Basemap` esri/dijit/Basemap class
  44. * - `BasemapGallery` esri/dijit/BasemapGallery class
  45. * - `BasemapLayer` esri/dijit/BasemapLayer class
  46. * - `makeBasemaps` function that makes a basemap gallery based on the settings provided
  47. */
  48. // Basemap related modules
  49. module.exports = { initBasemaps };