-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (64 loc) · 2.7 KB
/
Copy pathapp.js
File metadata and controls
91 lines (64 loc) · 2.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// alert("hehehe");
let iaddress = "google.com";
let mymap;
calling(iaddress)
async function calling(address){
let ipaddress;
let domain;
if( (address.match(/./g) || []).length !== 3){
domain = address;
}else{
ipaddress = address;
}
try {
const response = await fetch(`https://geo.ipify.org/api/v1?apiKey=at_anM34U2gCZBMishgNeyw1WiJy4dxY&ipAddress=${ipaddress}&domain=${domain}`);
const data = await response.json();
const locationcode = [data.location.lat , data.location.lng];
const address = data.ip;
const timezone = data.location.timezone;
const country = `${data.location.city}, ${data.location.country} ${data.location.postalCode}`;
const isp = data.isp;
getmap(locationcode);
addinfo(address,timezone,country,isp);
}catch(err){
let error = "<p class='error'> Please provide valid IP address or domain name </p>";
console.log("Invalid Input");
console.log(err);
document.querySelector(".search").insertAdjacentHTML("afterend", error)
}
}
function getmap(coordinates){
if(mymap!=undefined){
mymap.remove();
}
mymap = L.map('mapid').setView(coordinates, 13);
L.tileLayer("https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}", {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: "mapbox/streets-v11",
tileSize: 512,
zoomOffset: -1,
accessToken: "pk.eyJ1Ijoid2luc2VuIiwiYSI6ImNrZmRheWYzbzA0dDYyeHQ3NDkwbHZvN2UifQ.GnPvoiPZHPV-PqDNeJMPfw",
}).addTo(mymap);
var marker = L.marker(coordinates).addTo(mymap);
}
function getinput(){
let userinput = document.getElementById("id-search").value;
calling(userinput);
document.getElementById("id-search").value = "";
document.querySelector(".error").remove();
}
var input = document.getElementById("id-search");
input.addEventListener("keyup",function(event){
if(event.keyCode === 13){
event.preventDefault();
document.getElementById("mybtn").click();
}
});
function addinfo(address,timezone,country,isp){
document.querySelector(".info").innerHTML = "";
document.getElementById("ip").innerHTML = `${address}`;
document.getElementById("timezone").innerHTML = `${timezone}`;
document.getElementById("location").innerHTML = `${country}`;
document.getElementById("isp").innerHTML = `${isp}`;
}