MapBox API

Sentrale abstraksjoner i Mabox-API'et

Map-objektet


const map = new mapboxgl.Map({
            container: 'map', // container ID
            style: 'mapbox://styles/mapbox/streets-v11', // style URL
            center: [-74.5, 40], // starting position [lng, lat]
            zoom: 9 // starting zoom
        });

Map-objektets API

Options, f.eks. container, zoom

 

Instance members, f.eks. getBounds(), zoomIn()

 

Events, f.eks. click(), move()

 

 

 

 

 

accessToken

Gir tilgang til MapBox-ressursene. Må alltid være med. Du får ditt eget accessToken når du registrerer deg hos MapBox.

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Display a map on a webpage</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
    <link href="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.css" rel="stylesheet">
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.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>
        mapboxgl.accessToken = 'pk.xxxxx'; // Ditt eget access token
        const map = new mapboxgl.Map({
            container: 'map', // container ID
            style: 'mapbox://styles/mapbox/streets-v11', // style URL
            center: [-74.5, 40], // starting position [lng, lat]
            zoom: 9 // starting zoom
        });
    </script>

</body>

</html>

LngLatBounds

Brukes for å beskrive et geografisk rektangel (Bounding Box). Har en del "members", det vil si funksjoner for å hente ut enkelt-koordinater.

LngLat

Brukes for å lagre koordinater.

<script>
    mapboxgl.accessToken = 'pk.eyJ1Ijoic3ZlcnJlc3QiLCJhIjoiY2l1Y2Vq' +
               'xxxxxxxxxxxxxxx6c3ZteGozMyJ9.ieY0kEubUisIWVVwjZiuBg';
               
    const map = new mapboxgl.Map({
            container: 'map', // container ID
            style: 'mapbox://styles/mapbox/streets-v11', // style URL
            center: [-74.5, 40], // starting position [lng, lat]
            zoom: 9 // starting zoom
        });


    map.on('load', function () {
        var bounds = map.getBounds();
        console.log(bounds);
        console.log(bounds.getWest());
        console.log(bounds.getSouth());
    });
</script>