Initialize a map using a bounding box | Mapbox GL JS (original) (raw)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Initialize a map using a bounding box</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.12.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.12.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
    html,
    body,
    #map-wrapper-outer {
        width: 100%;
        height: 100%;
        overflow: hidden;
    }

    #map-wrapper-outer {
        display: 'flex';
        justify-content: center;
        align-items: center;
    }

    #map-wrapper-inner {
        position: relative;
        height: 100%;
        width: 100%;
    }

    #btn-container {
        position: absolute;
        top: 20px;
        display: flex;
        justify-content: center;
        gap: 10px;
        width: 100%;
        padding: 0 10px 0 10px;
        box-sizing: border-box;
    }

    .btn-control {
        font:
            bold 12px/20px 'Helvetica Neue',
            Arial,
            Helvetica,
            sans-serif;
        background-color: #3386c0;
        color: #fff;
        z-index: 1;
        border: none;
        width: 200px;
        cursor: pointer;
        padding: 10px 20px;
        border-radius: 3px;
    }

    .btn-control:hover {
        background-color: #4ea0da;
    }

    .btn-control.active::before {
        content: '✔';
        margin-right: 5px;
    }
</style>
<div id="btn-container">
    <button id="narrow" class="btn-control">Narrow</button>
    <button id="full" class="btn-control active">Full</button>
    <button id="wide" class="btn-control">Wide</button>
</div>
<div id="map-wrapper-outer">
    <div id="map-wrapper-inner">
        <div id="map"></div>
    </div>
</div>
<br>

<script>
    // TO MAKE THE MAP APPEAR YOU MUST
    // ADD YOUR ACCESS TOKEN FROM
    // https://account.mapbox.com
    mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
    let map;

    const initializeMap = () => {
        map = new mapboxgl.Map({
            container: 'map',
            bounds: [
                [-74.2709, 40.48972],
                [-73.7042, 40.93288]
            ], // bounding box (southwest corner, northeast corner)
            // by setting the bounds your map will automatically center around the bounding box
            fitBoundsOptions: {
                padding: 15 // padding to keep the bounds away from the edge of the map
            },
            style: 'mapbox://styles/mapbox/standard'
        });

        // add a geojson line layer with a polygon representing the bounds
        map.on('load', () => {
            map.addLayer({
                id: 'line-bounding-box',
                type: 'line',
                paint: {
                    'line-color': '#3386c0',
                    'line-width': 3,
                    'line-opacity': 0.9
                },
                source: {
                    type: 'geojson',
                    data: {
                        'type': 'Feature',
                        'properties': {},
                        'geometry': {
                            'type': 'Polygon',
                            'coordinates': [
                                [
                                    [-74.2709, 40.48972],
                                    [-74.2709, 40.93288],
                                    [-73.7042, 40.93288],
                                    [-73.7042, 40.48972],
                                    [-74.2709, 40.48972]
                                ]
                            ]
                        }
                    }
                }
            });
        });
    };

    // change the map wrapper size
    const resizeMap = ({ height, width }) => {
        const containerDiv = document.getElementById('map-wrapper-inner');

        containerDiv.style.width = width;
        containerDiv.style.height = height;

        if (map) map.remove();
        initializeMap();
    };

    const setActiveButton = (id) => {
        document.getElementsByClassName('active')[0].classList.remove('active');
        document.getElementById(id).classList.add('active');
    };

    // button click listeners
    document.getElementById('narrow').addEventListener('click', () => {
        resizeMap({ width: '30%', height: '100%' });
        setActiveButton('narrow');
    });

    document.getElementById('full').addEventListener('click', () => {
        resizeMap({ width: '100%', height: '100%' });
        setActiveButton('full');
    });

    document.getElementById('wide').addEventListener('click', () => {
        resizeMap({ width: '100%', height: '50%' });
        setActiveButton('wide');
    });

    initializeMap();
</script>

</body>
</html>