Skip to main content

GeoJSON Polygon

Add a polygon to the map using GeoJSON.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <link rel="stylesheet" href="https://maps-sdk.trimblemaps.com/v3/trimblemaps-3.17.0.css" />
  <script src="https://maps-sdk.trimblemaps.com/v3/trimblemaps-3.17.0.js"></script>
  <style>
    body {
      margin: 0;
      padding: 0;
    }
    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>
<body>
  <div id="map"></div>
    <script>
            TrimbleMaps.APIKey = 'YOUR_API_KEY_HERE';
            const map = new TrimbleMaps.Map({
            container: 'map', // container id
            style: TrimbleMaps.Common.Style.TRANSPORTATION, //hosted style id
            center: [-80.00633,40.44888], // starting position
            zoom: 14 // starting zoom
            });

            // This data can be hard-coded, loaded from a file, or pulled from a service
            const geoJsonData = {
                type: 'geojson',
                data: {
                    type: 'FeatureCollection',
                    features: [{
                        type: 'Feature',
                        properties: {},
                        geometry: {
                            type: 'Polygon',
                            coordinates: [
                                [
                                    [-80.007736,40.447704],
                                    [-80.004281,40.448271],
                                    [-80.003683,40.446575],
                                    [-80.007065,40.445767],
                                    [-80.007736,40.447704]
                                ]
                            ]
                        }
                    }]
                }
            };

            // Wait for the map to load before adding layers and data sources
            map.on('load', function () {
                // Add GeoJSON data source to the map
                map.addSource('hqSource', geoJsonData);

                // Add a layer to draw circles for each point in the data source
                map.addLayer({
                    id: 'hqPoly',
                    type: 'fill',
                    source: 'hqSource',
                    paint: {
                        'fill-color': '#000',
                        'fill-opacity': 0.5
                    }
                });
                // Add a black outline around the polygon.
                map.addLayer({
                    'id': 'hqoutline',
                    'type': 'line',
                    'source': 'hqSource',
                    'layout': {},
                    'paint': {
                        'line-color': '#000',
                        'line-width': 3
                    }
                });
            });
    </script>
  </body>
</html>
Last updated June 15, 2023.