Open-source layers usage

Open in CodeSandbox

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
    <!-- To make the map appear, you must add your apikey -->
    <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {ORM_DATA_SOURCE, ORM_LAYER, OSM_DATA_SOURCE, OSM_LAYER} from './common';
      import {LOCATION} from '../variables';

      window.map = null;

      main();
      async function main() {
        // Waiting for all api elements to be loaded
        await ymaps3.ready;
        const {YMap, YMapTileDataSource, YMapLayer, YMapControls} = ymaps3;

        const {YMapZoomControl} = await ymaps3.import('@yandex/ymaps3-default-ui-theme');

        const {WebMercator} = await ymaps3.import('@yandex/ymaps3-web-mercator-projection');

        // Initialize the map
        map = new YMap(
          // Pass the link to the HTMLElement of the container
          document.getElementById('app'),
          // Pass the map initialization parameters
          {
            location: LOCATION,
            projection: new WebMercator(),
            showScaleInCopyrights: true
          },
          [
            new YMapTileDataSource(OSM_DATA_SOURCE),
            new YMapLayer(OSM_LAYER),
            new YMapTileDataSource(ORM_DATA_SOURCE),
            new YMapLayer(ORM_LAYER),
            new YMapControls({position: 'right'}).addChild(new YMapZoomControl({}))
          ]
        );
      }
    </script>

    <!-- prettier-ignore -->
    <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
export const OSM_DATA_SOURCE = {
  id: 'osm',
  copyrights: ['© OpenStreetMap contributors'],
  raster: {
    type: 'ground',
    fetchTile: 'https://tile.openstreetmap.org/z/x/y.png'
  },
  zoomRange: {min: 0, max: 19},
  clampMapZoom: true
};
export const OSM_LAYER = {
  id: OSM_DATA_SOURCE.id,
  source: OSM_DATA_SOURCE.id,
  type: 'ground',
  options: {
    raster: {opacity: 0.7}
  }
};

export const ORM_DATA_SOURCE = {
  id: 'OpenRailwayMap',
  copyrights: ['© OpenRailwayMap'],
  raster: {
    type: 'ground',
    fetchTile: 'https://c.tiles.openrailwaymap.org/standard/z/x/y.png'
  }
};
export const ORM_LAYER = {
  id: ORM_DATA_SOURCE.id,
  source: ORM_DATA_SOURCE.id,
  type: 'ground'
};

ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', '@yandex/ymaps3-web-mercator-projection@0.0');

declare module '@yandex/ymaps3-types/import' {
  interface Import {
    (pkg: '@yandex/ymaps3-web-mercator-projection'): Promise<typeof import('@yandex/ymaps3-web-mercator-projection')>;
  }
}

ymaps3.ready.then(() => {
  ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', ['@yandex/ymaps3-default-ui-theme@0.0']);
});
import type {YMapLocationRequest} from '@yandex/ymaps3-types';

export const LOCATION: YMapLocationRequest = {
  center: [37.623082, 55.75254], // starting position [lng, lat]
  zoom: 9 // starting zoom
};