Synchronization of two maps

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://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="react, typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="react, typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {CUSTOMIZATION, BEHAVIOR, CAMERA} from './common';
      import {LOCATION} from '../variables';
      import type {MapEventUpdateHandler} from '@mappable-world/mappable-types';

      main();

      async function main() {
        // Wait for all API elements to be loaded
        await mappable.ready;

        // Get the app element which will contain the maps
        const map1Container = document.getElementById('map1');
        const map2Container = document.getElementById('map2');

        const {MMap, MMapDefaultSchemeLayer, MMapListener} = mappable;

        // Initialize the first map with behaviors and pass initialization parameters
        const map1 = new MMap(
          map1Container,
          {
            location: LOCATION,
            camera: CAMERA,
            behaviors: BEHAVIOR,
            showScaleInCopyrights: true
          },
          [new MMapDefaultSchemeLayer({})]
        );

        // Initialize the second map with behaviors and customization parameter to hide unnecessary objects
        const map2 = new MMap(
          map2Container,
          {
            location: LOCATION,
            camera: CAMERA,
            behaviors: BEHAVIOR,
            showScaleInCopyrights: true,
            // In this case, when we hide different objects, we must set the "vector" mode to avoid rerendering of the map after the first load
            mode: 'vector'
          },
          [new MMapDefaultSchemeLayer({customization: CUSTOMIZATION})]
        );

        // Update handler for synchronizing the map locations, camera, and behaviors
        const onUpdate: MapEventUpdateHandler = ({location, camera}) => {
          const newMapState = {
            location: {
              center: location.center,
              zoom: location.zoom
            },
            camera: camera
          };
          map1.setLocation(newMapState.location);
          map1.setCamera(newMapState.camera);
          map2.setLocation(newMapState.location);
          map2.setCamera(newMapState.camera);
        };

        // Create a map listener for the first map
        const listener1 = new MMapListener({
          onUpdate
        });
        map1.addChild(listener1);

        // Create a map listener for the second map
        const listener2 = new MMapListener({
          onUpdate
        });
        map2.addChild(listener2);
      }
    </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>
    <link rel="stylesheet" href="./common.css" />
  </head>
  <body>
    <div id="maps-container" class="maps-container">
      <div id="map1" class="map"></div>
      <div id="map2" class="map"></div>
    </div>
  </body>
</html>
<!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/react@17/umd/react.production.min.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
    <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://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="react, typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="react, typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
      import {BEHAVIOR, CAMERA, CUSTOMIZATION} from './common';
      import {LOCATION} from '../variables';
      import type {MapEventUpdateHandler, MMapCameraRequest, MMapLocationRequest} from '@mappable-world/mappable-types';

      main();

      async function main() {
          // For each object in the JS API, there is a React counterpart
          // To use the React version of the API, include the module @mappable-world/mappable-reactify
          const [mappableReact] = await Promise.all([mappable.import('@mappable-world/mappable-reactify'), mappable.ready]);
          const reactify = mappableReact.reactify.bindTo(React, ReactDOM);
          const {MMap, MMapDefaultSchemeLayer, MMapListener} = reactify.module(mappable);
          const {useState, useCallback} = React;

          function App() {
              const [mapState, setMapState] = useState<{location: MMapLocationRequest; camera: MMapCameraRequest}>({
                  location: LOCATION,
                  camera: CAMERA
              });

              // Update handler for synchronizing the map locations
              const updateHandler: MapEventUpdateHandler = useCallback(({camera, location}) => {
                  setMapState({
                      location: {
                          center: location.center,
                          zoom: location.zoom
                      },
                      camera
                  });
              }, []);

              return (
                  <div className="maps-container">
                      <div className="map">
                          {/* Initialize maps and pass initialization parameters */}
                          <MMap
                              location={mapState.location}
                              camera={mapState.camera}
                              behaviors={BEHAVIOR}
                              showScaleInCopyrights
                          >
                              {/* Add a map scheme layer */}
                              <MMapDefaultSchemeLayer />

                              {/* Add map listener for synchronizing maps location */}
                              <MMapListener onUpdate={updateHandler} />
                          </MMap>
                      </div>
                      <div className="map">
                          <MMap
                              location={mapState.location}
                              camera={mapState.camera}
                              behaviors={BEHAVIOR}
                              showScaleInCopyrights
                              // In this case, when we hide different objects, we must set the "vector" mode to avoid rerendering of the map after the first load
                              mode="vector"
                          >
                              {/* Add a map scheme layer with customization parameter to hide unnecessary objects */}
                              <MMapDefaultSchemeLayer customization={CUSTOMIZATION} />

                              {/* Add map listener for synchronizing maps location */}
                              <MMapListener onUpdate={updateHandler} />
                          </MMap>
                      </div>
                  </div>
              );
          }

          ReactDOM.render(
              <React.StrictMode>
                  <App />
              </React.StrictMode>,
              document.getElementById('app')
          );
      }
    </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>
    <link rel="stylesheet" href="./common.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
<!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/vue@3/dist/vue.global.js"></script>
    <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>

    <script src="https://js.api.mappable.world/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

    <script
      data-plugins="transform-modules-umd"
      data-presets="react, typescript"
      type="text/babel"
      src="./common.ts"
    ></script>
    <script
      data-plugins="transform-modules-umd"
      data-presets="react, typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {CUSTOMIZATION, BEHAVIOR, CAMERA} from './common';
      import {LOCATION} from '../variables';

      async function main() {
        // For each object in the JS API, there is a Vue counterpart
        // To use the Vue version of the API, include the module @mappable-world/mappable-vuefy
        const [mappableVue] = await Promise.all([mappable.import('@mappable-world/mappable-vuefy'), mappable.ready]);
        const vuefy = mappableVue.vuefy.bindTo(Vue);
        const {MMap, MMapDefaultSchemeLayer, MMapListener} = vuefy.module(mappable);

        const app = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapListener
          },
          setup() {
            const mapState = Vue.ref({
              location: LOCATION,
              camera: CAMERA
            });

            return {
              BEHAVIOR,
              CAMERA,
              CUSTOMIZATION,
              mapState
            };
          },
          methods: {
            // Update handler for synchronizing the map locations
            updateHandler({camera, location}) {
              this.mapState = {
                location: {
                  center: location.center,
                  zoom: location.zoom
                },
                camera
              };
            }
          },
          template: `
    <div class="maps-container">
      <div class="map">
        <!-- Initialize maps and pass initialization parameters -->
        <MMap :location="mapState.location" :camera="mapState.camera" :behaviors="BEHAVIOR" showScaleInCopyrights>
          <!-- Add a map scheme layer -->
          <MMapDefaultSchemeLayer/>

          <!-- Add map listener for synchronizing maps location -->
          <MMapListener @update="updateHandler" />
        </MMap>
      </div>
      <div class="map">
        <!-- In this case, when we hide different objects, we must set the "vector" mode to avoid rerendering of the map after the first load -->
        <MMap
          :location="mapState.location"
          :camera="mapState.camera"
          :behaviors="BEHAVIOR"
          showScaleInCopyrights
          mode="vector"
        >
          <!-- Add a map scheme layer with customization parameter to hide unnecessary objects -->
          <MMapDefaultSchemeLayer :customization="CUSTOMIZATION"/>

          <!-- Add map listener for synchronizing maps location -->
          <MMapListener @update="updateHandler" />
        </MMap>
      </div>
    </div>`
        });
        app.mount('#app');
      }

      main();
    </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>
    <link rel="stylesheet" href="./common.css" />
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
.maps-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  gap: 10px;
}

.map {
  width: 523px;
  height: 420px;
  border-radius: 8px;
  overflow: hidden;
}
import type {BehaviorType, VectorCustomization} from '@mappable-world/mappable-types';

// An array of enabled map behaviors
export const BEHAVIOR: BehaviorType[] = ['drag', 'scrollZoom', 'dblClick', 'mouseRotate', 'mouseTilt'];

export const CUSTOMIZATION: VectorCustomization = [
  {
    tags: {
      any: ['structure', 'admin', 'transit', 'poi']
    },
    stylers: [{visibility: 'off'}]
  }
];

export const CAMERA = {
  azimuth: 0,
  tilt: 0
};
import type {MMapLocationRequest} from '@mappable-world/mappable-types';

export const LOCATION: MMapLocationRequest = {
  center: [55.276012, 25.19505], // starting position [lng, lat]
  zoom: 15.4 // starting zoom
};