-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
59 lines (58 loc) · 1.89 KB
/
main.js
File metadata and controls
59 lines (58 loc) · 1.89 KB
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
// Formulario
const form = document.querySelector(".form");
const input = document.querySelector(".form__input");
const formBtn = document.querySelector(".form__btn")
// Informacion
const ipParagraph = document.getElementById("ip");
const locations = document.getElementById("location");
const timezone = document.getElementById("timezone");
const isp = document.getElementById("isp");
// Map
const titlesProvider = "https://tile.openstreetmap.org/{z}/{x}/{y}.png";
let lat;
let lng;
form.addEventListener("submit", (e) => {
e.preventDefault();
inputIP = input.value;
if (inputIP.trim() !== "" && inputIP !== undefined) {
pedirInfo(inputIP);
} else {
alert("Please enter a valid IP address");
}
});
const pedirInfo = async (ip) => {
try {
formBtn.classList.add("btn-load");
inputIP.includes("com") == true
? (secretAPI = await fetch(
`https://geo.ipify.org/api/v1?apiKey=at_oXuQi6cvttxp33vuNOMfhq3pHjbVa&domain=${ip}`
))
: (secretAPI = await fetch(
`https://geo.ipify.org/api/v1?apiKey=at_oXuQi6cvttxp33vuNOMfhq3pHjbVa&ipAddress=${ip}`
));
data = await secretAPI.json();
pintarInfo(data);
actualizarMapa(data);
} catch (error) {
console.log(error);
alert("Please enter a valid IP address");
} finally {
formBtn.classList.remove("btn-load");
}
};
const pintarInfo = (data) => {
console.log(data);
ipParagraph.textContent = data.ip;
locations.textContent = `${data.location.city}, ${data.location.region} ${data.location.postalCode}`;
timezone.textContent = "UTC " + data.location.timezone;
isp.textContent = data.isp;
};
const actualizarMapa = (data) => {
lat = data.location.lat;
lng = data.location.lng;
map.setView([lat, lng], 13);
};
var map = L.map("map").setView([37.38605, -122.08385], 13);
L.tileLayer(titlesProvider, {
maxZoom: 18,
}).addTo(map);