Skip to content

Add a Geometry

html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <link
      href="https://cdn.mapmetrics-atlas.net/versions/latest/mapmetrics-gl.css"
      rel="stylesheet"
    />
    <script src="https://cdn.mapmetrics-atlas.net/versions/latest/mapmetrics-gl.js"></script>
   
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
      }
      #map {
        min-height: 500px;
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      const accessToken = "<YOUR_ACCESS_TOKEN>";
 

      const map = new mapmetricsgl.Map({
        container: "map",
        style: `${accessToken}`,
        zoom: 11,
        center: [2.349902, 48.852966],
      }).addControl(new mapmetricsgl.NavigationControl(), "top-right");

      mapmetricsgl.setRTLTextPlugin(
        "https://cdn.mapmetrics-atlas.net/basemaps-assets/js/mapmetrics-gl-rtl-text.min.js"
      );

      const geoJsonFeature = {
        type: "Feature",
        properties: {},
        geometry: {
          type: "LineString",
          coordinates: [
            [2.319887, 48.90046],
            [2.329981, 48.901163],
            [2.38515, 48.902008],
            [2.394906, 48.898444],
            [2.397627, 48.894578],
            [2.398846, 48.887109],
            [2.408308, 48.880409],
            [2.41327, 48.872892],
            [2.413838, 48.864376],
            [2.416341, 48.849234],
            [2.412246, 48.834539],
            [2.422139, 48.835798],
            [2.41939, 48.842577],
            [2.42813, 48.841528],
            [2.447699, 48.844818],
            [2.463438, 48.842089],
            [2.467426, 48.838891],
            [2.467582, 48.833133],
            [2.462696, 48.81906],
            [2.458705, 48.81714],
            [2.438448, 48.818232],
            [2.421462, 48.824054],
            [2.406032, 48.827615],
            [2.390939, 48.826079],
            [2.379296, 48.821214],
            [2.363947, 48.816314],
            [2.345958, 48.816036],
            [2.331898, 48.817011],
            [2.332461, 48.818247],
            [2.292196, 48.827142],
            [2.279052, 48.83249],
            [2.272793, 48.82792],
            [2.263174, 48.83398],
            [2.255144, 48.83481],
            [2.251709, 48.838822],
            [2.250612, 48.845555],
            [2.239978, 48.849702],
            [2.224219, 48.853517],
            [2.228225, 48.865183],
            [2.231736, 48.869069],
            [2.245678, 48.876435],
            [2.25541, 48.874264],
            [2.258467, 48.880387],
            [2.277487, 48.877968],
            [2.282327, 48.883923],
            [2.291507, 48.889472],
            [2.319887, 48.90046],
          ],
        },
      };

      map.on("load", () => {
        map.addLayer({
          id: "route",
          type: "line",
          source: {
            type: "geojson",
            data: geoJsonFeature,
          },
          layout: {
            "line-join": "round",
            "line-cap": "round",
          },
          paint: {
            "line-color": "steelblue",
            "line-width": 4,
          },
        });
      });
    </script>
  </body>
</html>
jsx
import React, { useEffect, useRef } from 'react';
import mapmetricsgl from '@mapmetrics/mapmetrics-gl';
import '@mapmetrics/mapmetrics-gl/dist/mapmetrics-gl.css';

const AddGeometry = () => {
  const mapContainerRef = useRef(null);
  const mapRef = useRef(null);

  useEffect(() => {
    if (!mapContainerRef.current || mapRef.current) return;

    const accessToken = "<YOUR_ACCESS_TOKEN>";

    const geoJsonFeature = {
      type: "Feature",
      properties: {},
      geometry: {
        type: "LineString",
        coordinates: [
          [2.319887, 48.90046],
          [2.329981, 48.901163],
          [2.38515, 48.902008],
          [2.394906, 48.898444],
          [2.397627, 48.894578],
          [2.398846, 48.887109],
          [2.408308, 48.880409],
          [2.41327, 48.872892],
          [2.413838, 48.864376],
          [2.416341, 48.849234],
          [2.412246, 48.834539],
          [2.422139, 48.835798],
          [2.41939, 48.842577],
          [2.42813, 48.841528],
          [2.447699, 48.844818],
          [2.463438, 48.842089],
          [2.467426, 48.838891],
          [2.467582, 48.833133],
          [2.462696, 48.81906],
          [2.458705, 48.81714],
          [2.438448, 48.818232],
          [2.421462, 48.824054],
          [2.406032, 48.827615],
          [2.390939, 48.826079],
          [2.379296, 48.821214],
          [2.363947, 48.816314],
          [2.345958, 48.816036],
          [2.331898, 48.817011],
          [2.332461, 48.818247],
          [2.292196, 48.827142],
          [2.279052, 48.83249],
          [2.272793, 48.82792],
          [2.263174, 48.83398],
          [2.255144, 48.83481],
          [2.251709, 48.838822],
          [2.250612, 48.845555],
          [2.239978, 48.849702],
          [2.224219, 48.853517],
          [2.228225, 48.865183],
          [2.231736, 48.869069],
          [2.245678, 48.876435],
          [2.25541, 48.874264],
          [2.258467, 48.880387],
          [2.277487, 48.877968],
          [2.282327, 48.883923],
          [2.291507, 48.889472],
          [2.319887, 48.90046],
        ],
      },
    };

    const map = new mapmetricsgl.Map({
      container: mapContainerRef.current,
      style: `${accessToken}`,
      zoom: 11,
      center: [2.349902, 48.852966],
    });

    map.addControl(new mapmetricsgl.NavigationControl(), "top-right");

    mapmetricsgl.setRTLTextPlugin(
      "https://cdn.mapmetrics-atlas.net/basemaps-assets/js/mapmetrics-gl-rtl-text.min.js"
    );

    map.on("load", () => {
      map.addLayer({
        id: "route",
        type: "line",
        source: {
          type: "geojson",
          data: geoJsonFeature,
        },
        layout: {
          "line-join": "round",
          "line-cap": "round",
        },
        paint: {
          "line-color": "steelblue",
          "line-width": 4,
        },
      });
    });

    mapRef.current = map;

    return () => {
      map.remove();
      mapRef.current = null;
    };
  }, []);

  return (
    <div
      ref={mapContainerRef}
      style={{
        minHeight: '500px',
        height: '500px',
        width: '100%'
      }}
    />
  );
};

export default AddGeometry;