-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
68 lines (67 loc) · 2.03 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
mapboxgl.accessToken =
"pk.eyJ1Ijoia2Vzb2NpYWwiLCJhIjoiY2wxeTFsMDNnMDJjZDNkcWhmbDAwajQ3YiJ9.pARxiWrzCq-Apw5wTOlXHQ";
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/mapbox/navigation-night-v1",
center: [0, 18], // starting position
zoom: 5,
});
var markers = [];
var markersRepresentation = [];
var markersContainer = document.getElementById("markers");
console.log(map);
const actualizarMarkers = (markers) => {
markersContainer.innerHTML = "";
markers.forEach(({ longitud, latitud }, index) => {
var marker = document.createElement("div");
marker.classList.add("markerContenedor");
marker.innerHTML = `
<div class="marker">
<div>
Marcador: ${index}
</div>
<div> ${longitud}</div>
<div>${latitud}</div>
</div>
<button onclick="eliminarMarker(${index})"className="deleteMarker">x</button>
`;
markersContainer.appendChild(marker);
});
};
map.on("click", function (e) {
var longitud = e.lngLat.lng;
var latitud = e.lngLat.lat;
var m = new mapboxgl.Marker();
m._color = "#fefef";
m.setLngLat([longitud, latitud]).addTo(map);
const markerObject = {
longitud: longitud,
latitud: latitud,
};
markersRepresentation.push(markerObject);
markers.push(m);
console.log("Marcador agregado");
console.log("AllMarkers:");
console.log(markers);
console.log("AllMarkersContainer:");
console.log(markersRepresentation);
console.log("MarkerDeleted \n\n");
actualizarMarkers(markersRepresentation);
});
const eliminarMarker = (pos) => {
markers[pos].remove();
markersRepresentation = markersRepresentation.filter(
(e) => e != markersRepresentation[pos]
);
markers = markers.filter((e) => e != markers[pos]);
actualizarMarkers(markersRepresentation);
console.log("Marcador eliminado");
console.table("Pos:" + pos);
console.log("AllMarkers:");
console.log(markers);
console.log("AllMarkersContainer:");
console.log(markersRepresentation);
console.log("MarkerDeleted");
console.log(markers[pos]);
console.log("\n\n");
};