SOURCE

console 命令行工具 X clear

                    
>
console
	mapboxgl.accessToken = 'pk.eyJ1IjoiemhhbmdjaGVuMTk4OCIsImEiOiJjajYyeHMybzUxZDNlMzBqeHpxY3F4OTI0In0.GEHyHKuNf-hgOckcgOtYNg';
    const map = new mapboxgl.Map({
        container: 'map', // container ID
        // Choose from Mapbox's core styles, or make your own style with Mapbox Studio
        style: 'mapbox://styles/mapbox/satellite-v9', // style URL
        center: [-91.874, 42.76], // starting position [lng, lat]
        zoom: 12 // starting zoom
    });
    const geojson={
        "id": "4bd43768e6ca24a2d2182e67f19e1993",
        "type": "Feature",
        "properties": {},
        "geometry": {
            "coordinates": [
                [
                    [
                        -91.8777765502924,
                        42.773960248879916
                    ],
                    [
                        -91.84378759765568,
                        42.77421226051405
                    ],
                    [
                        -91.84378759765568,
                        42.745224200524746
                    ],
                    [
                        -91.87829153442324,
                        42.74497207096152
                    ],
                    [
                        -91.8777765502924,
                        42.773960248879916
                    ]
                ]
            ],
            "type": "Polygon"
        }
    }
    map.on('load',()=>{
      // Add a data source containing GeoJSON data.
      map.addSource('maine', {
      'type': 'geojson',
      'data': geojson
      });

      // Add a new layer to visualize the polygon.
      map.addLayer({
        'id': 'maine',
        'type': 'fill',
        'source': 'maine', // reference the data source
        'layout': {},
        'paint': {
        'fill-color': '#0080ff', // blue color fill
        'fill-opacity': 0.5
        }
      });
    })

    const draw = new MapboxDraw({
        displayControlsDefault: false,
        // Select which mapbox-gl-draw control buttons to add to the map.
        controls: {
            polygon: true,
            trash: true
        },
        // Set mapbox-gl-draw to draw by default.
        // The user does not have to click the polygon control button first.
        defaultMode: 'draw_polygon'
    });
    map.addControl(draw);

    map.on('draw.create', updateArea);
    map.on('draw.delete', updateArea);
    map.on('draw.update', updateArea);
    map.on('draw.modechange', (e)=>{
        console.log(1111)
    });

    function updateArea(e) {
        //draw.changeMode('draw_polygon')
        const data = draw.getAll();
        const answer = document.getElementById('calculated-area');
        if (data.features.length > 0) {
            const area = turf.area(data);
            // Restrict the area to 2 decimal points.
            const rounded_area = Math.round(area * 100) / 100;
            answer.innerHTML = `<p><strong>${rounded_area}</strong></p><p>square meters</p>`;
        } else {
            answer.innerHTML = '';
            if (e.type !== 'draw.delete')
                alert('Click the map to draw a polygon.');
        }
    }




<div id="map"></div>
<div class="calculation-box">
    <button onclick='click()'>编辑</button>
    <p>Click the map to draw a polygon.</p>
    <div id="calculated-area"></div>
</div>



body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }

    .calculation-box {
        height: 75px;
        width: 150px;
        position: absolute;
        bottom: 40px;
        left: 10px;
        background-color: rgba(255, 255, 255, 0.9);
        padding: 15px;
        text-align: center;
    }

    p {
        font-family: 'Open Sans';
        margin: 0;
        font-size: 13px;
    }