Skip to content

Google map to Mapmetrics โ€‹

Switch from Google Maps to Mapmetrics โ€‹


Skill LevelLanguage
๐ŸŸง๐ŸŸง๐ŸŸง Intermediate๐Ÿง  JavaScript

๐Ÿ“ Prerequisite โ€‹

Familiarity with front-end development concepts.

Are you using Google Maps and want to switch to Mapmetrics?
Youโ€™ve come to the right place!

This tutorial shows you how to use the Mapmetrics GL JS JavaScript library to:

  • โœ… Create a web map
  • ๐Ÿ“ Add a marker to the map
  • ๐Ÿ’ฌ Attach a popup to the marker

All using methods similar to those in the Google Maps JavaScript API.


๐ŸŒ Live Demo โ€‹

๐Ÿงช View the live map demo here: Open Map Demo


Getting started โ€‹

This guide assumes that you are already familiar with the Google Maps JavaScript API and with front-end web development concepts including HTML, CSS, and JavaScript.

To complete this tutorial, you will need:

๐Ÿ”ง Requirements โ€‹

  • A Mapmetrics access token: Your access tokens are on the Access Token page of your Developer Console.
  • Mapmetrics GL JS: Mapmetrics GL JS is a JavaScript API for building web maps.
  • A text editor: Use the editor of your choice for writing HTML, CSS, and JavaScript.

Create a webpage โ€‹

  1. Open your text editor and create a new file called index.html.
  2. Paste the following code to set up a map-enabled web page:
html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <link
      href="https://gateway.mapmetrics-atlas.net/assets/css/mapmetrics-gl.css"
      rel="stylesheet"
    />
    <script src="https://gateway.mapmetrics-atlas.net/assets/js/mapmetrics-gl.js"></script>
    <script src="https://gateway.mapmetrics-atlas.net/assets/js/pmtiles.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 protocol = new pmtiles.Protocol();
      mapmetrics.addProtocol("pmtiles", protocol.tile);
      const map = new mapmetrics.Map({
        container: "map",
        style: `https://gateway.mapmetrics-atlas.net/styles/?fileName=fe60a384-0e5a-4960-bf04-b6ca740bc20c/AtlasGlow.json&token=${accessToken}`,
        zoom: 2,
        center: [2.3210938, 48.7965913],
      }).addControl(new mapmetrics.NavigationControl(), "top-right");
    </script>
  </body>
</html>

Initialize a web map โ€‹

Google Maps Example โ€‹

html
<script>
  const map = new google.maps.Map(document.getElementById("map"), {
    mapTypeId: "roadmap",
    center: { lat: 64.1436456, lng: -21.9270884 },
    zoom: 13,
  });
</script>

Mapmetrics GL JS Example โ€‹

html
<script>
  const accessToken = "YOUR_ACCESS_TOKEN";
  const protocol = new pmtiles.Protocol();
  mapmetrics.addProtocol("pmtiles", protocol.tile);

  const map = new mapmetrics.Map({
    container: "map",
    style: `https://gateway.mapmetrics-atlas.net/styles/?fileName=fe60a384-0e5a-4960-bf04-b6ca740bc20c/AtlasGlow.json&token=${accessToken}`,
    zoom: 2,
    center: [2.3210938, 48.7965913],
  }).addControl(new mapmetrics.NavigationControl(), "top-right");
</script>
  • container: HTML element to contain the map.
  • Style options:
    1. AtlasGlow (Light): https://gateway.mapmetrics-atlas.net/styles/?fileName=fe60a384-0e5a-4960-bf04-b6ca740bc20c/AtlasGlow.json&token=${accessToken}
    2. MoonTrace (Dark): https://gateway.mapmetrics-atlas.net/styles/?fileName=fe60a384-0e5a-4960-bf04-b6ca740bc20c/MoonTrace.json&token=${accessToken}
  • center and zoom define the map's starting position.

๐Ÿ’พ Save your file and open it in a browser to preview.

๐Ÿ”— Open Map Demo


Add a Marker โ€‹

Google Maps Example โ€‹

html
<script>
  const map = new google.maps.Map(document.getElementById("map"), {
    mapTypeId: "roadmap",
    center: { lat: 64.1436456, lng: -21.9270884 },
    zoom: 13,
  });

  const marker = new google.maps.Marker({
    position: { lat: 64.1436456, lng: -21.9270884 },
    title: "Reykjavik Roasters - Coffee Shop",
    map: map,
  });
</script>

Mapmetrics GL JS Example โ€‹

html
<script>
  new mapmetrics.Marker().setLngLat([2.349902, 48.852966]).addTo(map);
</script>
  • setLngLat(): Set the marker location.
  • addTo(): Attach marker to the map.

Add interactivity โ€‹

Google Maps: Using InfoWindow โ€‹

html
<script>
  const infowindow = new google.maps.InfoWindow({
    content: `<h3>Reykjavik Roasters</h3><p>A good coffee shop</p>`,
  });

  marker.addListener("click", () => {
    infowindow.open(map, marker);
  });
</script>

Mapmetrics GL JS: Using Popup โ€‹

html
<script>
  const popup = new mapmetrics.Popup().setHTML(
    `<h3>Hello Mapmetrics</h3><p>A good coffee shop</p>`
  );

  new mapmetrics.Marker()
    .setLngLat([2.349902, 48.852966])
    .setPopup(popup)
    .addTo(map);
</script>

๐Ÿ”— Open Popup Demo


Final Product Example โ€‹

html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <link
      href="https://gateway.mapmetrics-atlas.net/assets/css/mapmetrics-gl.css"
      rel="stylesheet"
    />
    <script src="https://gateway.mapmetrics-atlas.net/assets/js/mapmetrics-gl.js"></script>
    <script src="https://gateway.mapmetrics-atlas.net/assets/js/pmtiles.js"></script>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
      }
      #map {
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>
      const accessToken = "YOUR_ACCESS_TOKEN";
      const protocol = new pmtiles.Protocol();
      mapmetrics.addProtocol("pmtiles", protocol.tile);

      const map = new mapmetrics.Map({
        container: "map",
        style: `https://gateway.mapmetrics-atlas.net/styles/?fileName=fe60a384-0e5a-4960-bf04-b6ca740bc20c/MoonTrace.json&token=${accessToken}`,
        zoom: 10,
        center: [2.3210938, 48.7965913],
      }).addControl(new mapmetrics.NavigationControl(), "top-right");
    </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 GoogleToMapmetrics = () => {
  const mapContainerRef = useRef(null);
  const mapRef = useRef(null);

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

    const accessToken = "<YOUR_ACCESS_TOKEN>";

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

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

    mapRef.current = map;

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

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

export default GoogleToMapmetrics;

Next Steps โ€‹

๐ŸŽ‰ Congratulations!

You've built a fully interactive map using Mapmetrics GL JS.

โœ… Recap โ€‹

  • Map setup using Mapmetrics GL JS
  • Marker and Popup creation

๐Ÿ“š Keep Exploring โ€‹

Visit the official documentation: