Skip to content

Commit 2821ab0

Browse files
committed
costum marker
1 parent ebc7973 commit 2821ab0

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

src/App.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import Map1 from './components/Map1'
33
import Map2 from './components/Map2'
44
import Map3 from './components/Map3'
55
import Map4 from './components/Map4'
6+
import Map5 from './components/Map5'
7+
import Map6 from './components/Map6'
68

79
function App() {
810

911
return (
1012
<>
11-
<Map3 />
13+
<Map6 />
1214
</>
1315
)
1416
};

src/components/Map5/index.jsx

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useEffect, useRef } from 'react';
2+
import Mapbox from 'mapbox-gl';
3+
4+
import MapContainer from '../MapContainer'
5+
6+
let map = null;
7+
let marker = null;
8+
9+
const Map5 = () => {
10+
11+
Mapbox.accessToken = process.env.MAPBOX_API_KEY;
12+
13+
const mapElement = useRef(null);
14+
15+
useEffect(() => {
16+
map = new Mapbox.Map({
17+
container: mapElement.current,
18+
style: 'mapbox://styles/mapbox/dark-v10',
19+
zoom: 10,
20+
center: [12.492285, 41.890466]
21+
})
22+
.on('click', event => handleMapClick(event))
23+
24+
}, []);
25+
26+
const handleMapClick = event => {
27+
28+
let el = document.createElement('div');
29+
el.style.display = 'block';
30+
el.style.height = '40px';
31+
el.style.width = '40px';
32+
el.style.backgroundImage = 'url("https://upload.wikimedia.org/wikipedia/commons/4/4b/McDonald%27s_logo.svg")';
33+
el.style.backgroundSize = '40px 40px'
34+
el.addEventListener('click', event => console.log("Heihei"))
35+
36+
const newMarker = new Mapbox.Marker(el)
37+
.setLngLat(event.lngLat);
38+
39+
if (marker !== null) { marker.remove() };
40+
41+
newMarker.addTo(map);
42+
marker = newMarker;
43+
44+
};
45+
46+
return (
47+
<>
48+
<MapContainer ref={mapElement}></MapContainer>
49+
</>
50+
)
51+
};
52+
53+
export default Map5;

src/components/Map6/index.jsx

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { useEffect, useRef } from 'react';
2+
import Mapbox from 'mapbox-gl';
3+
4+
import MapContainer from '../MapContainer'
5+
6+
let map = null;
7+
let marker = null;
8+
9+
const Map6 = () => {
10+
11+
Mapbox.accessToken = process.env.MAPBOX_API_KEY;
12+
13+
const mapElement = useRef(null);
14+
15+
useEffect(() => {
16+
map = new Mapbox.Map({
17+
container: mapElement.current,
18+
style: 'mapbox://styles/mapbox/streets-v11',
19+
zoom: 5,
20+
center: [12.492285, 41.890466]
21+
})
22+
23+
}, []);
24+
25+
26+
return (
27+
<>
28+
<MapContainer ref={mapElement}></MapContainer>
29+
</>
30+
)
31+
};
32+
33+
export default Map6;

0 commit comments

Comments
 (0)