generic.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Map from 'ol/Map';
  2. import TileLayer from 'ol/layer/Tile';
  3. import XYZ from 'ol/source/XYZ';
  4. import { MapLayerRegistryItem, MapLayerOptions, GrafanaTheme2 } from '@grafana/data';
  5. export interface XYZConfig {
  6. url: string;
  7. attribution: string;
  8. minZoom?: number;
  9. maxZoom?: number;
  10. }
  11. const sampleURL = 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer';
  12. export const defaultXYZConfig: XYZConfig = {
  13. url: sampleURL + '/tile/{z}/{y}/{x}',
  14. attribution: `Tiles © <a href="${sampleURL}">ArcGIS</a>`,
  15. };
  16. export const xyzTiles: MapLayerRegistryItem<XYZConfig> = {
  17. id: 'xyz',
  18. name: 'XYZ Tile layer',
  19. isBaseMap: true,
  20. create: async (map: Map, options: MapLayerOptions<XYZConfig>, theme: GrafanaTheme2) => ({
  21. init: () => {
  22. const cfg = { ...options.config };
  23. if (!cfg.url) {
  24. cfg.url = defaultXYZConfig.url;
  25. cfg.attribution = cfg.attribution ?? defaultXYZConfig.attribution;
  26. }
  27. return new TileLayer({
  28. source: new XYZ({
  29. url: cfg.url,
  30. attributions: cfg.attribution, // singular?
  31. }),
  32. minZoom: cfg.minZoom,
  33. maxZoom: cfg.maxZoom,
  34. });
  35. },
  36. registerOptionsUI: (builder) => {
  37. builder
  38. .addTextInput({
  39. path: 'config.url',
  40. name: 'URL template',
  41. description: 'Must include {x}, {y} or {-y}, and {z} placeholders',
  42. settings: {
  43. placeholder: defaultXYZConfig.url,
  44. },
  45. })
  46. .addTextInput({
  47. path: 'config.attribution',
  48. name: 'Attribution',
  49. settings: {
  50. placeholder: defaultXYZConfig.attribution,
  51. },
  52. });
  53. },
  54. }),
  55. };
  56. export const genericLayers = [xyzTiles];