Projekt

Obecné

Profil

Stáhnout (1.54 KB) Statistiky
| Větev: | Tag: | Revize:
1
import React, { useEffect, useRef, useState } from 'react'
2
import mapboxgl, { LngLatLike, Popup } from 'mapbox-gl'
3

    
4

    
5
const coords = {
6
    lng: 7.3700,
7
    lat: 60.0400
8
}
9

    
10
// Adapted from
11
// https://docs.mapbox.com/help/tutorials/use-mapbox-gl-js-with-react/
12
const MapBoxExample = () => {
13

    
14
    const [lng, setLng] = useState(coords.lng)
15
    const [lat, setLat] = useState(coords.lat)
16
    const [zoom, setZoom] = useState(1)
17

    
18
    // TODO type
19
    const map = useRef<any>(null)
20
    const mapContainer = useRef<any>(null)
21

    
22
    // Function to add markers to the map
23
    const addMarkers = () => {
24
        for (let i = 1; i <= 10; i += 1) { //
25
            new mapboxgl.Marker()
26
                .setLngLat([i * .5 + coords.lng, i * .5 + coords.lat])
27
                .setPopup(new Popup()
28
                    .setHTML(`<h3>HELLO POPUP ${ i } ☀️🍕😂😅</h3>`))
29
                .addTo(map.current as mapboxgl.Map)
30
        }
31
    }
32

    
33
    useEffect(() => {
34
        if (map.current) {
35
            return
36
        }
37

    
38
        map.current = new mapboxgl.Map({
39
            container: mapContainer.current,
40
            center: [lng, lat],
41
            accessToken: process.env.REACT_APP_MAPS_API_KEY,
42
            style: 'mapbox://styles/mapbox/streets-v11',
43
            zoom: zoom
44
        })
45

    
46
        map.current.addControl(new mapboxgl.NavigationControl())
47

    
48
        addMarkers()
49
    })
50

    
51
    console.log('Whatup')
52

    
53
    return (
54
        <div>
55
            <h1>Hello Map</h1>
56
            <div ref={ mapContainer } style={ { height: '700px' } }/>
57
        </div>
58
    )
59
}
60

    
61
export default MapBoxExample
    (1-1/1)