|
1 | 1 | console.log("Practica 7");
|
2 | 2 | /** Variables Globales */
|
3 | 3 | let map;
|
| 4 | +let btnEliminar; |
4 | 5 | let marcadoresArray = [];
|
5 |
| -let marcador; |
6 |
| -let lat = 18.9038592; |
7 |
| -let lng = -98.9736192; |
| 6 | +let markersArray = []; |
| 7 | +let lat = 18.8690435; |
| 8 | +let lng = -97.0984946; |
| 9 | + |
| 10 | +let nombre = ""; |
| 11 | +const colors = ["blue", "red", "black", "green", "orange"]; |
| 12 | +let colorIndex = 0; |
| 13 | + |
| 14 | +/** Clase global para los valores de Latitud y Longitud. */ |
| 15 | +let myLatLng = { lat: lat, lng: lng }; |
| 16 | + |
| 17 | +/** Opciones iniciales del mapa. */ |
| 18 | +let mapOptions = { |
| 19 | + center: myLatLng, |
| 20 | + zoom: 8, |
| 21 | +}; |
8 | 22 |
|
9 | 23 | function iniciarMap() {
|
10 |
| - map = new google.maps.Map(document.getElementById("map"), { |
11 |
| - center: { lat: 18.9038592, lng: -98.9736192 }, |
12 |
| - zoom: 8, |
13 |
| - }); |
| 24 | + btnEliminar = document.getElementById("btnEliminar"); |
| 25 | + btnEliminar.disabled = true; |
| 26 | + map = new google.maps.Map(document.getElementById("map"), mapOptions); |
14 | 27 |
|
15 |
| - marcador = new google.maps.Marker({ |
16 |
| - position: { lat: 18.9038592, lng: -98.9736192 }, |
17 |
| - map: map, |
18 |
| - draggable: true, |
| 28 | + let marker = new google.maps.Marker({ |
| 29 | + position: myLatLng, |
19 | 30 | animation: google.maps.Animation.DROP,
|
20 |
| - title: "Marcador 1", |
| 31 | + icon: |
| 32 | + "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png", |
| 33 | + title: "Universidad del Valle de Orizaba", |
| 34 | + }); |
| 35 | + |
| 36 | + marker.setMap(map); |
| 37 | + |
| 38 | + let infowindow = new google.maps.InfoWindow(); |
| 39 | + infowindow.setContent( |
| 40 | + "<strong>" + |
| 41 | + marker.title + |
| 42 | + "</strong><br /><strong> Coordenadas:</strong>" + |
| 43 | + marker.position |
| 44 | + ); |
| 45 | + |
| 46 | + marker.addListener("click", () => { |
| 47 | + infowindow.open(map, marker); |
21 | 48 | });
|
22 | 49 | }
|
23 | 50 |
|
24 | 51 | function agregarMarcadores() {
|
25 |
| - let long = document.getElementById("txtLong").value; |
26 |
| - let lat = document.getElementById("txtLat").value; |
27 |
| - let nombre = document.getElementById("txtNombre").value; |
28 |
| - |
29 |
| - marcador = new google.maps.Marker({ |
30 |
| - position: { lat: parseFloat(lat), lng: parseFloat(long) }, |
31 |
| - map: map, |
32 |
| - draggable: true, |
| 52 | + document.getElementById("divTablaMarcadores").innerHTML = ""; |
| 53 | + let txtLat = document.getElementById("txtLat").value; |
| 54 | + let txtLng = document.getElementById("txtLng").value; |
| 55 | + let txtNombre = document.getElementById("txtNombre").value; |
| 56 | + |
| 57 | + lat = txtLat; |
| 58 | + lng = txtLng; |
| 59 | + nombre = txtNombre; |
| 60 | + myLatLng = { lat: parseFloat(lat), lng: parseFloat(lng) }; |
| 61 | + |
| 62 | + const svgMarker = { |
| 63 | + path: |
| 64 | + "M10.453 14.016l6.563-6.609-1.406-1.406-5.156 5.203-2.063-2.109-1.406 1.406zM12 2.016q2.906 0 4.945 2.039t2.039 4.945q0 1.453-0.727 3.328t-1.758 3.516-2.039 3.070-1.711 2.273l-0.75 0.797q-0.281-0.328-0.75-0.867t-1.688-2.156-2.133-3.141-1.664-3.445-0.75-3.375q0-2.906 2.039-4.945t4.945-2.039z", |
| 65 | + fillColor: colors[colorIndex++ % colors.length], |
| 66 | + fillOpacity: 1.0, |
| 67 | + strokeWeight: 0, |
| 68 | + rotation: 0, |
| 69 | + scale: 2, |
| 70 | + anchor: new google.maps.Point(15, 30), |
| 71 | + }; |
| 72 | + |
| 73 | + let marker = new google.maps.Marker({ |
| 74 | + position: myLatLng, |
33 | 75 | animation: google.maps.Animation.DROP,
|
34 |
| - icon: |
35 |
| - "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png", |
| 76 | + icon: svgMarker, |
36 | 77 | title: nombre,
|
37 | 78 | });
|
38 | 79 |
|
| 80 | + marker.setMap(map); |
| 81 | + |
39 | 82 | let infowindow = new google.maps.InfoWindow();
|
40 |
| - infowindow.setContent(nombre); |
| 83 | + infowindow.setContent( |
| 84 | + "<strong>" + |
| 85 | + marker.title + |
| 86 | + "</strong><br /><strong> Coordenadas:</strong>" + |
| 87 | + marker.position |
| 88 | + ); |
41 | 89 |
|
42 |
| - marcador.addListener("click", () => { |
43 |
| - infowindow.open(map, marcador); |
| 90 | + marker.addListener("click", () => { |
| 91 | + infowindow.open(map, marker); |
44 | 92 | });
|
45 | 93 |
|
46 |
| - marcadoresArray.push(marcador); |
| 94 | + /** Agregar Marcador */ |
| 95 | + markersArray.push(marker); |
| 96 | + agregarMarcador(); |
| 97 | + mostrarTablaMarcadores(); |
| 98 | + limpiarInputs(); |
47 | 99 | }
|
48 | 100 |
|
49 |
| -function eliminarMarcadores(params) { |
50 |
| - marcadoresArray.forEach((e) => { |
| 101 | +/** Elimina Marcadores del mapa. */ |
| 102 | +function eliminarMarcadores() { |
| 103 | + markersArray.forEach((e) => { |
51 | 104 | e.setMap(null);
|
52 | 105 | });
|
| 106 | + markersArray = []; |
53 | 107 | marcadoresArray = [];
|
| 108 | + document.getElementById("divTablaMarcadores").innerHTML = ""; |
| 109 | + btnEliminar.disabled = true; |
| 110 | +} |
| 111 | + |
| 112 | +/** Arma el arreglo de marcadores. */ |
| 113 | +function agregarMarcador() { |
| 114 | + marcadoresArray.push({ |
| 115 | + _nombre: nombre, |
| 116 | + _lat: lat, |
| 117 | + _lng: lng, |
| 118 | + }); |
| 119 | + btnEliminar.disabled = false; |
| 120 | +} |
| 121 | + |
| 122 | +function mostrarTablaMarcadores() { |
| 123 | + let resultado = |
| 124 | + "<table><thead> <tr> <th>Nombre</th> <th>Latitud</th> <th>Longitud</th> </tr> </thead>"; |
| 125 | + resultado += "<tbody>"; |
| 126 | + marcadoresArray.forEach((e) => { |
| 127 | + resultado += "<tr>"; |
| 128 | + resultado += "<td>" + e._nombre + "</td>"; |
| 129 | + resultado += "<td>" + e._lat + "</td>"; |
| 130 | + resultado += "<td>" + e._lng + "</td>"; |
| 131 | + resultado += "</tr>"; |
| 132 | + }); |
| 133 | + resultado += "</tbody> </table>"; |
| 134 | + document.getElementById("divTablaMarcadores").innerHTML = resultado; |
| 135 | +} |
| 136 | + |
| 137 | +/** Limpia los inputs del formularios. */ |
| 138 | +function limpiarInputs() { |
| 139 | + document.getElementById("txtNombre").value = ""; |
| 140 | + document.getElementById("txtLng").value = ""; |
| 141 | + document.getElementById("txtLat").value = ""; |
54 | 142 | }
|
0 commit comments