Create a rectangle

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="../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 type { RectangleProps } from './common';
      import { boundsToGeometry } from './common';
      import { LOCATION, RECTANGLE_COORDINATES, RECTANGLE_STYLE } from '../variables';


      window.map = null;

      main();
      async function main() {
          // Waiting for all api elements to be loaded
          await mappable.ready;
          const { MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapFeature, MMapComplexEntity } = mappable;
          // 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({}),
                  // Add a layer of geo objects to display the polygons
                  new MMapDefaultFeaturesLayer({})
              ]
          );

          class Rectangle extends MMapComplexEntity<RectangleProps> {
              private _rectangle?: MMapFeature;

              constructor(props: RectangleProps) {
                  super(props);
                  const { bounds, ...rest } = props;

                  this._rectangle = new MMapFeature({
                      geometry: boundsToGeometry(bounds),
                      ...rest
                  });
                  this.addChild(this._rectangle);
              }
          }

          const rectangle = new Rectangle({
              bounds: RECTANGLE_COORDINATES,
              style: RECTANGLE_STYLE
          });

          map.addChild(rectangle);
      }
    </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>
<!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 {RectangleProps} from './common';
      import {boundsToGeometry} from './common';
      import {LOCATION, RECTANGLE_COORDINATES, RECTANGLE_STYLE} 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, MMapFeature, MMapDefaultFeaturesLayer} = reactify.module(mappable);

        function Rectangle({bounds, ...featureProps}: RectangleProps) {
          return <MMapFeature geometry={boundsToGeometry(bounds)} {...featureProps} />;
        }

        function App() {
          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 to display the rectangle */}
              <MMapDefaultFeaturesLayer />

              <Rectangle bounds={RECTANGLE_COORDINATES} style={RECTANGLE_STYLE} />
            </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>
  </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="../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 type {RectangleProps} from './common';
      import {boundsToGeometry} from './common';
      import {LOCATION, RECTANGLE_COORDINATES, RECTANGLE_STYLE} 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, MMapFeature} = vuefy.module(mappable);

        const Rectangle = Vue.defineComponent({
          name: 'Rectangle',
          components: {MMapFeature},
          props: {
            bounds: {type: Array, required: true}
          },
          setup(props: RectangleProps) {
            const rectangleGeometry = Vue.computed(() => {
              return boundsToGeometry(props.bounds);
            });

            // Extract all props except bounds to pass to MMapFeature
            const featureProps = Vue.computed(() => {
              const {bounds, ...rest} = props;
              return rest;
            });

            return {rectangleGeometry, featureProps};
          },
          template: `<MMapFeature :geometry="rectangleGeometry" v-bind="featureProps" />`
        });

        const app = Vue.createApp({
          components: {
            MMap,
            MMapDefaultSchemeLayer,
            MMapDefaultFeaturesLayer,
            Rectangle
          },
          setup() {
            const refMap = (ref) => {
              window.map = ref?.entity;
            };
            return {
              LOCATION,
              RECTANGLE_COORDINATES,
              RECTANGLE_STYLE,
              refMap
            };
          },
          template: `
          <!--Initialize the map and pass initialization parameters-->
          <MMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
            <!--Add a map scheme layer-->
            <MMapDefaultSchemeLayer/>
            <!-- Add a layer of geo objects to display the rectangle -->
            <MMapDefaultFeaturesLayer/>

            <Rectangle :bounds="RECTANGLE_COORDINATES" :style="RECTANGLE_STYLE" />
          </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>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>
import type {LngLatBounds, MMapFeatureProps} from '@mappable-world/mappable-types';

export type RectangleProps = {
  bounds: LngLatBounds;
} & Omit<MMapFeatureProps, 'geometry'>;

export function boundsToGeometry(bounds: LngLatBounds) {
  // Determine the four coordinates of a rectangle
  const [leftLowerCorner, rightUpperCorner] = bounds;
  const leftUpperCorner = [leftLowerCorner[0], rightUpperCorner[1]];
  const rightLowerCorner = [rightUpperCorner[0], leftLowerCorner[1]];

  const rectangleGeometry = {
    type: 'Polygon',
    coordinates: [[leftLowerCorner, leftUpperCorner, rightUpperCorner, rightLowerCorner]]
  };

  return rectangleGeometry;
}
import type {MMapLocationRequest, LngLatBounds, DrawingStyle} from '@mappable-world/mappable-types';

export const LOCATION: MMapLocationRequest = {
  center: [55.169, 25.1141], // starting position [lng, lat]
  zoom: 10 // starting zoom
};

export const RECTANGLE_COORDINATES: LngLatBounds = [
  [55.2, 25.16],
  [55.12, 25.04]
];

export const RECTANGLE_STYLE: DrawingStyle = {
  stroke: [
    {
      color: '#ff1919f8',
      width: 5
    }
  ],
  fill: '#196dff95'
};