---
updatedAt: 2025-09-17T22:29:08.000Z
agentTools:
  projectIndex: https://docs.vizionapi.com/llms.txt
---

# Add Container Trace response to a map

```javascript JavaScript
const map = new mapboxgl.Map({
  container: "map",
  style: "mapbox://styles/mapbox/streets-v12",
  center: [120.82, 22],
  zoom: 4,
});

// Create GeoJSON lineString with the given coordinates
function createGeometry(coordinates) {
  const geometry = {
    type: "LineString",
    coordinates: coordinates,
  };

  for (let i = 0; i < coordinates.length - 1; i++) {
    const startLng = coordinates[i][0];
    const endLng = coordinates[i + 1][0];

    // Check if the line crosses the antimeridian
    if ((endLng > startLng && endLng - startLng >= 180) || 
        (endLng < startLng && startLng - endLng >= 180)) {
      // Adjust the longitude to ensure proper line rendering across the antimeridian
      if (endLng > startLng) {
        geometry.coordinates[i + 1][0] -= 360;
      } else {
        geometry.coordinates[i + 1][0] += 360;
      }
    }
  }

  return geometry;
}


// Function to handle map load event
map.on("load", () => {
  // Request to retrieve container trace data
  axios.get('https://prod.vizionapi.com/references/e7946bf4-255e-484d-8c9b-01365fffa31a/trace', {
    headers: {
      'X-API-Key': 'YOUR_API_KEY_HERE'
    }
  })
  .then(function (response) {
    // Extract container trace data from the response
    const containerTraceResponse = response.data;

    // Initialize an empty array to store coordinates
    const coordinates = [];

    // Loop through trace data and extract latitude and longitude for each point
    containerTraceResponse.trace.forEach((point) => {
      const { latitude, longitude } = point;
      coordinates.push([longitude, latitude]); // GeoJSON format: [longitude, latitude]
    });

    const polylineData = {
      type: "Feature",
      properties: {},
      geometry: createGeometry(coordinates),
    };

    // Add polyline data as a new source
    map.addSource("polyline", {
      type: "geojson",
      data: polylineData,
    });

    // Add polyline layer
    map.addLayer({
      id: "polyline",
      type: "line",
      source: "polyline",
      layout: {
        "line-join": "round",
        "line-cap": "round",
      },
      paint: {
        "line-color": "#FF0000",
        "line-width": 5,
      },
    });
  })
  .catch(function (error) {
    console.log(error);
  });
});

```

```json Response Example
{"success":true}
```

# Initialize a map

<!-- javascript@1-7 -->

Creates a new MapboxGL map object, specifying the container element, map style, center coordinates, and initial zoom level.

# Check if line crosses antimeridian



Avoid wrapping issues on the map by checking if the route crosses the 180th meridian.

# Make a request to List container trace endpoint

<!-- javascript@9-15 -->

Executes a GET request to retrieve container trace data once map loads.

# Process response data and render polyline

<!-- javascript@16-64 -->

Extracts container trace data from the response, processes it to extract coordinates, and renders a polyline on the map.