Create a circle

Open in CodeSandbox

The circle on the map can be displayed on the map as a MMapFeature. To get GeoJSON in the form of a circle, use turf.circle.

<!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>

    <script crossorigin src="https://cdn.jsdelivr.net/npm/@turf/turf@7.1.0/turf.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">
      import {
        CIRCLE_CENTER,
        CIRCLE_DASHED_CENTER,
        CIRCLE_DASHED_STYLE,
        CIRCLE_RADIUS,
        CIRCLE_STYLE,
        CIRCLE_WITHOUT_STROKE_CENTER,
        CIRCLE_WITHOUT_STROKE_STYLE,
        LOCATION,
        getCircleGeoJSON
      } 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} = 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({}), new MMapDefaultFeaturesLayer({})]
        );

        // Create default circle
        const circle = new MMapFeature({
          geometry: getCircleGeoJSON(CIRCLE_CENTER, CIRCLE_RADIUS),
          style: CIRCLE_STYLE
        });
        map.addChild(circle);

        // Create circle with custom styles
        const circleDashed = new MMapFeature({
          geometry: getCircleGeoJSON(CIRCLE_DASHED_CENTER, CIRCLE_RADIUS),
          style: CIRCLE_DASHED_STYLE
        });
        map.addChild(circleDashed);

        // Create circle without stroke
        const circleWithoutStroke = new MMapFeature({
          geometry: getCircleGeoJSON(CIRCLE_WITHOUT_STROKE_CENTER, CIRCLE_RADIUS),
          style: CIRCLE_WITHOUT_STROKE_STYLE
        });

        map.addChild(circleWithoutStroke);
      }
    </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>

    <script crossorigin src="https://cdn.jsdelivr.net/npm/@turf/turf@7.1.0/turf.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="react, typescript" type="text/babel">
      import {
        CIRCLE_CENTER,
        CIRCLE_DASHED_CENTER,
        CIRCLE_DASHED_STYLE,
        CIRCLE_RADIUS,
        CIRCLE_STYLE,
        CIRCLE_WITHOUT_STROKE_CENTER,
        CIRCLE_WITHOUT_STROKE_STYLE,
        LOCATION,
        getCircleGeoJSON
      } 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, MMapFeature} = reactify.module(mappable);
        const {useMemo} = React;

        function App() {
          const circleGeometry = useMemo(() => getCircleGeoJSON(CIRCLE_CENTER, CIRCLE_RADIUS), []);
          const circleDashedGeometry = useMemo(() => getCircleGeoJSON(CIRCLE_DASHED_CENTER, CIRCLE_RADIUS), []);
          const circleWithoutStrokeGeometry = useMemo(
            () => getCircleGeoJSON(CIRCLE_WITHOUT_STROKE_CENTER, CIRCLE_RADIUS),
            []
          );
          return (
            // Initialize the map and pass initialization parameters
            <MMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
              {/* Add a map scheme layer */}
              <MMapDefaultSchemeLayer />
              <MMapDefaultFeaturesLayer />
              <MMapFeature geometry={circleGeometry} style={CIRCLE_STYLE} />
              <MMapFeature geometry={circleDashedGeometry} style={CIRCLE_DASHED_STYLE} />
              <MMapFeature geometry={circleWithoutStrokeGeometry} style={CIRCLE_WITHOUT_STROKE_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 crossorigin src="https://cdn.jsdelivr.net/npm/@turf/turf@7.1.0/turf.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="typescript"
      type="text/babel"
      src="../variables.ts"
    ></script>
    <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
      import {
        CIRCLE_CENTER,
        CIRCLE_DASHED_CENTER,
        CIRCLE_DASHED_STYLE,
        CIRCLE_RADIUS,
        CIRCLE_STYLE,
        CIRCLE_WITHOUT_STROKE_CENTER,
        CIRCLE_WITHOUT_STROKE_STYLE,
        LOCATION,
        getCircleGeoJSON
      } 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 app = Vue.createApp({
          components: {MMap, MMapDefaultSchemeLayer, MMapDefaultFeaturesLayer, MMapFeature},
          setup() {
            const refMap = (ref) => {
              window.map = ref?.entity;
            };
            const circleGeometry = Vue.ref(getCircleGeoJSON(CIRCLE_CENTER, CIRCLE_RADIUS));
            const circleDashedGeometry = Vue.ref(getCircleGeoJSON(CIRCLE_DASHED_CENTER, CIRCLE_RADIUS));
            const circleWithoutStrokeGeometry = Vue.ref(
              getCircleGeoJSON(CIRCLE_WITHOUT_STROKE_CENTER, CIRCLE_RADIUS)
            );
            return {
              LOCATION,
              CIRCLE_CENTER,
              CIRCLE_DASHED_CENTER,
              CIRCLE_DASHED_STYLE,
              CIRCLE_WITHOUT_STROKE_STYLE,
              CIRCLE_WITHOUT_STROKE_CENTER,
              CIRCLE_STYLE,
              circleGeometry,
              circleDashedGeometry,
              circleWithoutStrokeGeometry,
              refMap
            };
          },
          template: `
            <MMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
                <MMapDefaultSchemeLayer />
                <MMapDefaultFeaturesLayer />
                <MMapFeature :geometry="circleGeometry" :style="CIRCLE_STYLE" />
                <MMapFeature :geometry="circleDashedGeometry" :style="CIRCLE_DASHED_STYLE" />
                <MMapFeature :geometry="circleWithoutStrokeGeometry" :style="CIRCLE_WITHOUT_STROKE_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 {DrawingStyle, LngLat, PolygonGeometry, MMapLocationRequest} from '@mappable-world/mappable-types';

declare global {
  const turf: typeof import('@turf/turf');
}

export const LOCATION: MMapLocationRequest = {
  center: [55.44279, 25.24613], // starting position [lng, lat]
  zoom: 12 // starting zoom
};

export const CIRCLE_CENTER: LngLat = [55.43825, 25.25385];
export const CIRCLE_RADIUS = 1500;
export const CIRCLE_STYLE: DrawingStyle = {simplificationRate: 0};

export const CIRCLE_DASHED_CENTER: LngLat = [55.42801, 25.23222];
export const CIRCLE_DASHED_STYLE: DrawingStyle = {
  simplificationRate: 0,
  stroke: [{color: '#006efc', width: 4, dash: [5, 10]}],
  fill: 'rgba(56, 56, 219, 0.5)'
};

export const CIRCLE_WITHOUT_STROKE_CENTER: LngLat = [55.48025, 25.22385];
export const CIRCLE_WITHOUT_STROKE_STYLE: DrawingStyle = {simplificationRate: 0, stroke: []};

export const getCircleGeoJSON = (center: LngLat, radiusMeters: number): PolygonGeometry => {
  const {geometry} = turf.circle(center, radiusMeters, {units: 'meters'});
  return geometry as PolygonGeometry;
};