Skip to content

Simple Map with NPM Installation

This guide demonstrates how to set up a MapMetrics map using the NPM package. Follow these steps to get started.

Prerequisites

Before you begin, ensure you have:

Installation with NPM

1. Install the Package

Run the following command in your project directory to install the MapMetrics-gl package:

bash
npm install @mapmetrics/mapmetrics-gl

2. Import the Package

In your JavaScript or TypeScript file, import the MapMetrics-gl package and its CSS:

javascript
import mapmetricsgl from "@mapmetrics/mapmetrics-gl";
import "@mapmetrics/mapmetrics-gl/dist/mapmetrics-gl.css";

3. Create the Map Container

Add a <div> element to your HTML where the map will be rendered:

html
<div id="map"></div>

4. Initialize the Map

Use the following JavaScript to initialize the map. Replace <YOUR_ACCESS_TOKEN> with your actual API key and style URL:

javascript
const accessToken = `<YOUR_ACCESS_TOKEN>`;

const map = new mapmetricsgl.Map({
  container: "map",
  style: `${accessToken}`,
  center: [2.349902, 48.852966],
  zoom: 11,
  minZoom: 1,
  maxZoom: 24,
  attributionControl: false,
  cooperativeGestures: true,
});

Complete Example

Here's a complete example using React:

javascript
import React, { useEffect, useRef } from "react";
import mapmetricsgl from "@mapmetrics/mapmetrics-gl";
import "@mapmetrics/mapmetrics-gl/dist/mapmetrics-gl.css";

const App = () => {
  const mapContainerRef = useRef<HTMLDivElement>(null);
  const mapRef = useRef<mapmetricsgl.Map | null>(null);

  const accessToken = `<YOUR_ACCESS_TOKEN>`;

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

    const map = new mapmetricsgl.Map({
      container: mapContainerRef.current,
      style: `${accessToken}`,
      center: [2.349902, 48.852966],
      zoom: 11,
      minZoom: 1,
      maxZoom: 24,
      attributionControl: false,
      cooperativeGestures: true,
    });

    mapRef.current = map;

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

  return (
    <div
      id="map"
      ref={mapContainerRef}
      style={{ height: "100vh", width: "100%" }}
    ></div>
  );
};

export default App;

For more information, visit the MapMetrics GitHub repository.

Map Container Example

Here's an example of how the map container looks in action: