---
title: "Position markers at different altitudes with 3D terrain | JavaScript maps SDK"
description: "Position draggable markers at different altitudes above ground and sea."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs-qmgv.pages.dev/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Position markers at different altitudes with 3D terrain

This example demonstrates how to place markers at **different altitudes** floating above 3D terrain using MapTiler SDK v5:

-   **Altitude positioning:** Set marker altitude in meters using `.setAltitude(altitude, { relativeTo: "sea" | "ground" })`.
    -   **Sea level (`relativeTo: "sea"`):** Position markers at absolute elevations (ASL). Markers stay at a fixed global height regardless of the terrain underneath.
    -   **Ground level (`relativeTo: "ground"`):** Position markers relative to the terrain surface directly below (AGL). Markers rise and fall dynamically following the underlying slopes and ridges.
-   **Vertical ground lines:** Enable `.setGroundLine(true)` to draw a dynamic vertical connector line from each floating marker down to the surface of the 3D terrain.
-   **Interactive dragging:** Drag any marker across the terrain (`draggable: true`). Notice how sea-level markers keep their fixed elevation while their ground lines stretch or contract, while ground-relative markers glide over mountain contours at a constant clearance.

The scene centers on the dramatic mountain amphitheater of Glencoe, Scotland, featuring four nearby markers: a mountain rescue helicopter at 720 m ASL, a ridge lookout at 580 m ASL, a survey drone hovering at 150 m AGL, and a valley basecamp stationed at 40 m AGL.

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
import { Map, MapStyle, config, Popup, Marker } from "@maptiler/sdk";
import "@maptiler/sdk/dist/maptiler-sdk.css";

config.apiKey = "YOUR_MAPTILER_API_KEY";

// Initialize map with 3D terrain centered on Glencoe amphitheater
const map = new Map({
  container: "map",
  style: MapStyle.HYBRID,
  center: [-5.005, 56.661],
  zoom: 13.7,
  pitch: 60,
  bearing: -15,
  terrain: true,
  terrainControl: true,
});

map.on("load", () => {
  // 1. High Altitude Marker (720 m ASL): Rescue Helicopter
  const helicopter = new Marker({
    lngLat: [-5.001, 56.663],
    shape: "maptiler",
    size: "m",
    icon: "hospital",
    innerColor: "#E11D48",
    outerColor: "#FFFFFF",
    title: "Mountain Rescue Helicopter (720 m ASL)",
    draggable: true,
  });
  map.addMarker(helicopter);
  helicopter.setAltitude(720, { relativeTo: "sea" }).setGroundLine(true);
  helicopter.setPopup(
    new Popup({ offset: 30 }).setHTML(
      "<strong>Rescue Helicopter</strong><br>Altitude: 720 m ASL (Sea level)<br><em>Draggable marker</em>",
    ),
  );

  // 2. Mid-High Altitude Marker (580 m ASL): Mountain Ridge Lookout
  const ridge = new Marker({
    lngLat: [-5.007, 56.656],
    shape: "maptiler",
    size: "m",
    icon: "flag",
    innerColor: "#0070F3",
    outerColor: "#FFFFFF",
    title: "Ridge Lookout (580 m ASL)",
    draggable: true,
  });
  map.addMarker(ridge);
  ridge.setAltitude(580, { relativeTo: "sea" }).setGroundLine(true);
  ridge.setPopup(
    new Popup({ offset: 30 }).setHTML(
      "<strong>Ridge Lookout</strong><br>Altitude: 580 m ASL (Sea level)<br><em>Draggable marker</em>",
    ),
  );

  // 3. Ground-Relative Marker (150 m AGL): Survey Drone
  const drone = new Marker({
    lngLat: [-5.004, 56.659],
    shape: "circle",
    size: "m",
    content: "D",
    contentColor: "#FFFFFF",
    innerColor: "#8B5CF6",
    outerColor: "#FFFFFF",
    title: "Survey Drone (150 m AGL - Ground)",
    draggable: true,
  });
  map.addMarker(drone);
  drone.setAltitude(150, { relativeTo: "ground" }).setGroundLine(true);
  drone.setPopup(
    new Popup({ offset: 30 }).setHTML(
      "<strong>Survey Drone</strong><br>Altitude: 150 m AGL (Relative to ground)<br><em>Draggable marker</em>",
    ),
  );

  // 4. Ground-Relative Marker (40 m AGL): Valley Basecamp
  const basecamp = new Marker({
    lngLat: [-5.007, 56.664],
    shape: "bubble-circle",
    size: "m",
    innerColor: "#10B981",
    outerColor: "#FFFFFF",
    title: "Valley Basecamp (40 m AGL - Ground)",
    draggable: true,
  });
  map.addMarker(basecamp);
  basecamp.setAltitude(40, { relativeTo: "ground" }).setGroundLine(true);
  basecamp.setPopup(
    new Popup({ offset: 30 }).setHTML(
      "<strong>Valley Basecamp</strong><br>Altitude: 40 m AGL (Relative to ground)<br><em>Draggable marker</em>",
    ),
  );
});
```

#### index.html

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="robots" content="noindex" />
    <title>3D markers at different altitudes</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div id="map"></div>
    <div class="panel">
      <strong>3D Markers with Altitude</strong>
      <div class="panel-desc">
        Markers positioned at different elevations relative to sea level (ASL) or the ground (AGL),
        with vertical ground lines. Drag any marker to observe how its elevation adjusts over 3D
        terrain.
      </div>
      <div class="legend">
        <div class="legend-item">
          <span class="legend-dot" style="background: #e11d48"></span>
          <span><strong>720 m ASL</strong> – Rescue Helicopter (Sea level)</span>
        </div>
        <div class="legend-item">
          <span class="legend-dot" style="background: #0070f3"></span>
          <span><strong>580 m ASL</strong> – Ridge Lookout (Sea level)</span>
        </div>
        <div class="legend-item">
          <span class="legend-dot" style="background: #8b5cf6"></span>
          <span><strong>150 m AGL</strong> – Survey Drone (Ground)</span>
        </div>
        <div class="legend-item">
          <span class="legend-dot" style="background: #10b981"></span>
          <span><strong>40 m AGL</strong> – Valley Basecamp (Ground)</span>
        </div>
      </div>
    </div>
    <script type="module" src="./main.js"></script>
  </body>
</html>
```

#### style.css

```css
body {
  margin: 0;
  padding: 0;
}
#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}
.panel {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 10;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  padding: 10px 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  font-family:
    system-ui,
    -apple-system,
    sans-serif;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 300px;
}
.panel-desc {
  color: #475569;
  font-size: 12px;
  line-height: 1.4;
}
.legend {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 4px;
  font-size: 12px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
}
.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
```

### Basic JavaScript setup

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>3D markers at different altitudes</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <link
      href="https://cdn.maptiler.com/maptiler-sdk-js/v5.0.0/maptiler-sdk.css"
      rel="stylesheet"
    />
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
      }
      .panel {
        position: absolute;
        top: 10px;
        left: 10px;
        z-index: 10;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 8px;
        padding: 10px 14px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        font-family:
          system-ui,
          -apple-system,
          sans-serif;
        font-size: 13px;
        display: flex;
        flex-direction: column;
        gap: 6px;
        max-width: 300px;
      }
      .panel-desc {
        color: #475569;
        font-size: 12px;
        line-height: 1.4;
      }
      .legend {
        display: flex;
        flex-direction: column;
        gap: 4px;
        margin-top: 4px;
        font-size: 12px;
      }
      .legend-item {
        display: flex;
        align-items: center;
        gap: 8px;
      }
      .legend-dot {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        flex-shrink: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div class="panel">
      <strong>3D Markers with Altitude</strong>
      <div class="panel-desc">
        Markers positioned at different elevations relative to sea level (ASL) or the ground (AGL),
        with vertical ground lines. Drag any marker to observe how its elevation adjusts over 3D
        terrain.
      </div>
      <div class="legend">
        <div class="legend-item">
          <span class="legend-dot" style="background: #e11d48"></span>
          <span><strong>720 m ASL</strong> – Rescue Helicopter (Sea level)</span>
        </div>
        <div class="legend-item">
          <span class="legend-dot" style="background: #0070f3"></span>
          <span><strong>580 m ASL</strong> – Ridge Lookout (Sea level)</span>
        </div>
        <div class="legend-item">
          <span class="legend-dot" style="background: #8b5cf6"></span>
          <span><strong>150 m AGL</strong> – Survey Drone (Ground)</span>
        </div>
        <div class="legend-item">
          <span class="legend-dot" style="background: #10b981"></span>
          <span><strong>40 m AGL</strong> – Valley Basecamp (Ground)</span>
        </div>
      </div>
    </div>

    <script type="module">
      import * as maptilersdk from "https://cdn.maptiler.com/maptiler-sdk-js/v5.0.0/maptiler-sdk.min.mjs";

      maptilersdk.config.apiKey = "YOUR_MAPTILER_API_KEY";

      // Initialize map with 3D terrain centered on Glencoe amphitheater
      const map = new maptilersdk.Map({
        container: "map",
        style: maptilersdk.MapStyle.HYBRID,
        center: [-5.005, 56.661],
        zoom: 13.7,
        pitch: 60,
        bearing: -15,
        terrain: true,
        terrainControl: true,
      });

      map.on("load", () => {
        // 1. High Altitude Marker (720 m ASL): Rescue Helicopter
        const helicopter = new maptilersdk.Marker({
          lngLat: [-5.001, 56.663],
          shape: "maptiler",
          size: "m",
          icon: "hospital",
          innerColor: "#E11D48",
          outerColor: "#FFFFFF",
          title: "Mountain Rescue Helicopter (720 m ASL)",
          draggable: true,
        });
        map.addMarker(helicopter);
        helicopter.setAltitude(720, { relativeTo: "sea" }).setGroundLine(true);
        helicopter.setPopup(
          new maptilersdk.Popup({ offset: 30 }).setHTML(
            "<strong>Rescue Helicopter</strong><br>Altitude: 720 m ASL (Sea level)<br><em>Draggable marker</em>",
          ),
        );

        // 2. Mid-High Altitude Marker (580 m ASL): Mountain Ridge Lookout
        const ridge = new maptilersdk.Marker({
          lngLat: [-5.007, 56.656],
          shape: "maptiler",
          size: "m",
          icon: "flag",
          innerColor: "#0070F3",
          outerColor: "#FFFFFF",
          title: "Ridge Lookout (580 m ASL)",
          draggable: true,
        });
        map.addMarker(ridge);
        ridge.setAltitude(580, { relativeTo: "sea" }).setGroundLine(true);
        ridge.setPopup(
          new maptilersdk.Popup({ offset: 30 }).setHTML(
            "<strong>Ridge Lookout</strong><br>Altitude: 580 m ASL (Sea level)<br><em>Draggable marker</em>",
          ),
        );

        // 3. Ground-Relative Marker (150 m AGL): Survey Drone
        const drone = new maptilersdk.Marker({
          lngLat: [-5.004, 56.659],
          shape: "circle",
          size: "m",
          content: "D",
          contentColor: "#FFFFFF",
          innerColor: "#8B5CF6",
          outerColor: "#FFFFFF",
          title: "Survey Drone (150 m AGL - Ground)",
          draggable: true,
        });
        map.addMarker(drone);
        drone.setAltitude(150, { relativeTo: "ground" }).setGroundLine(true);
        drone.setPopup(
          new maptilersdk.Popup({ offset: 30 }).setHTML(
            "<strong>Survey Drone</strong><br>Altitude: 150 m AGL (Relative to ground)<br><em>Draggable marker</em>",
          ),
        );

        // 4. Ground-Relative Marker (40 m AGL): Valley Basecamp
        const basecamp = new maptilersdk.Marker({
          lngLat: [-5.007, 56.664],
          shape: "bubble-circle",
          size: "m",
          innerColor: "#10B981",
          outerColor: "#FFFFFF",
          title: "Valley Basecamp (40 m AGL - Ground)",
          draggable: true,
        });
        map.addMarker(basecamp);
        basecamp.setAltitude(40, { relativeTo: "ground" }).setGroundLine(true);
        basecamp.setPopup(
          new maptilersdk.Popup({ offset: 30 }).setHTML(
            "<strong>Valley Basecamp</strong><br>Altitude: 40 m AGL (Relative to ground)<br><em>Draggable marker</em>",
          ),
        );
      });
    </script>
  </body>
</html>
```

## Related examples

### [Display marker](https://docs-qmgv.pages.dev/sdk-js/examples/default-marker/)

Tutorials

Learn how to add a default marker to your map.

### [Create a draggable marker](https://docs-qmgv.pages.dev/sdk-js/examples/drag-a-marker/)

Examples

Create a draggable marker and display its map coordinates.

### [Show trace position Elevation profile control](https://docs-qmgv.pages.dev/sdk-js/examples/elevation-profile-control-marker/)

Examples

Synchronize a moving map marker with elevation profile cursor.

### [Marker shapes and customization](https://docs-qmgv.pages.dev/sdk-js/examples/marker-custom-shapes/)

Examples

Configure and create customizable, draggable markers in real-time with custom shapes, embedded letters, emojis, sizes, colors, and shadows.

### [Marker animations and states](https://docs-qmgv.pages.dev/sdk-js/examples/marker-animation-states/)

Examples

Create animated markers with interactive UI states and transitions.
