Bounds by rspn

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="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 {ANIMATION_PROPS, getBounds} from './common';
      import {LOCATION, MARKERS_COORDINATES} from '../variables';

      window.map = null;

      main();
      async function main() {
          // Waiting for all api elements to be loaded
          await mappable.ready;
          const {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapControls, MMapControl} = mappable;
          const {MMapDefaultMarker} = await mappable.import('@mappable-world/mappable-default-ui-theme');

          class ZoomToButtons extends mappable.MMapComplexEntity<{}> {
              private _element!: HTMLButtonElement;
              private _detachDom!: () => void;

              // Method for create a DOM control element
              _createElement() {
                  // Create a root element
                  const button = document.createElement('button');
                  button.classList.add('button');
                  button.innerText = 'Zoom to bounds';
                  button.onclick = () => {
                      const bounds = getBounds(MARKERS_COORDINATES);
                      this.root.setLocation({...ANIMATION_PROPS, bounds});
                  };

                  return button;
              }

              // Method for attaching the control to the map
              _onAttach() {
                  this._element = this._createElement();
                  this._detachDom = mappable.useDomContext(this, this._element, this._element);
              }

              // Method for detaching control from the map
              _onDetach() {
                  this._detachDom();
                  this._detachDom = undefined;
                  this._element = undefined;
              }
          }

          // Initialize the map
          map = new MMap(
              // Pass the link to the HTMLElement of the container
              document.getElementById('app'),
              // Pass the map initialization parameters
              {location: LOCATION, showScaleInCopyrights: true},
              // Add a map scheme layer
              [new MMapDefaultSchemeLayer({}), new MMapDefaultFeaturesLayer({})]
          );

          MARKERS_COORDINATES.forEach((coordinates) => {
              map.addChild(
                  new MMapDefaultMarker({
                      iconName: 'fallback',
                      size: 'normal',
                      coordinates
                  })
              );
          });

          const control = new MMapControl().addChild(new ZoomToButtons({}));
          map.addChild(new MMapControls({position: 'bottom'}, [control]));
      }
    </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/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="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="react, typescript" type="text/babel">
      import type {MMapLocationRequest} from '@mappable-world/mappable-types';
      import {ANIMATION_PROPS, getBounds} from './common';
      import {LOCATION, MARKERS_COORDINATES} from '../variables';

      window.map = null;

      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, MMapDefaultFeaturesLayer, MMapControls, MMapControl} =
          reactify.module(mappable);
        const {MMapDefaultMarker} = await reactify.module(
          await mappable.import('@mappable-world/mappable-default-ui-theme')
        );

        const {useState, useCallback} = React;

        function App() {
          const [location, setLocation] = useState < MMapLocationRequest > LOCATION;

          const onClick = useCallback(() => {
            const bounds = getBounds(MARKERS_COORDINATES);
            setLocation({...ANIMATION_PROPS, bounds});
          }, []);

          return (
            // Initialize the map and pass initialization parameters
            <MMap location={location} showScaleInCopyrights={true} ref={(x) => (map = x)}>
              {/* Add a map scheme layer */}
              <MMapDefaultSchemeLayer />

              {/* Add a layer of geo objects */}
              <MMapDefaultFeaturesLayer />

              {MARKERS_COORDINATES.map((coordinates, index) => (
                <MMapDefaultMarker key={index} coordinates={coordinates} iconName="fallback" size="normal" />
              ))}

              <MMapControls position="bottom">
                <MMapControl>
                  <button className="button" type="button" onClick={onClick}>
                    Zoom to bounds
                  </button>
                </MMapControl>
              </MMapControls>
            </MMap>
          );
        }

        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>
    <!-- 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="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 {ANIMATION_PROPS, getBounds} from './common';
      import {LOCATION, MARKERS_COORDINATES} from '../variables';

      window.map = null;

      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, MMapDefaultFeaturesLayer, MMapControls, MMapControl, MMapListener} =
          vuefy.module(mappable);
        const {MMapDefaultMarker} = await vuefy.module(
          await mappable.import('@mappable-world/mappable-default-ui-theme')
        );

        const app = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapDefaultFeaturesLayer,
            MMapControls,
            MMapControl,
            MMapDefaultMarker
          },
          setup() {
            const refMap = (ref) => {
              window.map = ref?.entity;
            };
            const location = Vue.ref(LOCATION);

            const onClick = () => {
              const bounds = getBounds(MARKERS_COORDINATES);
              location.value = {...ANIMATION_PROPS, bounds};
            };

            return {MARKERS_COORDINATES, refMap, location, onClick};
          },
          template: `
            <MMap :location="location" :showScaleInCopyrights="true" :ref="refMap">
                <MMapDefaultSchemeLayer />
                
                <MMapDefaultFeaturesLayer />

                <template v-for="(coordinates, index) in MARKERS_COORDINATES" :key="index">
                  <MMapDefaultMarker 
                    iconName="fallback"
                    size="normal"
                    :coordinates="coordinates"
                  />
                </template>

                <MMapControls position="bottom">
                  <MMapControl>
                    <button class="button" type="button" @click="onClick">
                      Zoom to bounds
                    </button>
                  </MMapControl>
                </MMapControls>
            </MMap>`
        });
        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>
.button {
  background-color: #ffffff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  padding: 12px 16px;
}
import type {LngLatBounds, MMapLocationRequest} from '@mappable-world/mappable-types';

mappable.ready.then(() => {
  mappable.import.registerCdn(
    'https://cdn.jsdelivr.net/npm/{package}',
    '@mappable-world/mappable-default-ui-theme@0.0'
  );
});

export const ANIMATION_PROPS: Partial<MMapLocationRequest> = {duration: 2000, easing: 'ease-in-out'};

export function getBounds(coordinates: number[][]): LngLatBounds {
  let minLat = Infinity,
    minLng = Infinity;
  let maxLat = -Infinity,
    maxLng = -Infinity;

  for (const coords of coordinates) {
    const lat = coords[1];
    const lng = coords[0];

    if (lat < minLat) minLat = lat;
    if (lat > maxLat) maxLat = lat;
    if (lng < minLng) minLng = lng;
    if (lng > maxLng) maxLng = lng;
  }

  return [
    [minLng, minLat],
    [maxLng, maxLat]
  ] as LngLatBounds;
}
import type {LngLat, MMapLocationRequest} from '@mappable-world/mappable-types';

export const LOCATION: MMapLocationRequest = {
  center: [55.1666, 25.0628], // starting position [lng, lat]
  zoom: 15.3 // starting zoom
};

export const MARKERS_COORDINATES: Array<LngLat> = [
  [55.1016, 25.0351],
  [55.1135, 25.0574],
  [55.1295, 25.0749],
  [55.1635, 25.0865],
  [55.1857, 25.0821],
  [55.2064, 25.0669],
  [55.1659, 25.0639],
  [55.1829, 25.0683],
  [55.1527, 25.0665],
  [55.1532, 25.0728],
  [55.1748, 25.0479],
  [55.1946, 25.0401],
  [55.2332, 25.0496],
  [55.2328, 25.0927],
  [55.2093, 25.0847],
  [55.2081, 25.0569],
  [55.1342, 25.0463],
  [55.1436, 25.0675],
  [55.1541, 25.0812],
  [55.1582, 25.0889],
  [55.1728, 25.0701],
  [55.1791, 25.0778],
  [55.1405, 25.0774],
  [55.2151, 25.033],
  [55.143, 25.0334]
];