-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
128 lines (104 loc) · 3.95 KB
/
script.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
let searchBtn = document.querySelector(".submit-button");
let loadingAnimation = document.querySelector('.loader');
let descBox = document.querySelector('.details');
let boxContent = document.querySelector('.inner-content');
//FUNCTIONS TO SHOW AND HIDE THE LOADER
function showLoader() {
loadingAnimation.classList.add('show');
boxContent.style.display = "none";
}
function hideLoader() {
loadingAnimation.classList.remove('show');
boxContent.style.display = "flex";
}
// map datas
const map = L.map('map').setView([0, 0], 1);
var tileUrl = 'https://tile.openstreetmap.org/{z}/{x}/{y}.png';
var attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
var tiles = L.tileLayer(tileUrl, { attribution });
tiles.addTo(map);
//making a marker
var marker = L.marker([0, 0]).addTo(map);
marker.setLatLng([0, 0]);
function getLocation() {
//ipify map variables
ipContent = document.getElementById("ip-address-no").value;
let text1 = document.querySelector('.input-session p');
text1.innerHTML = "";
var regEx = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
let result = regEx.test(ipContent);
if (ipContent == "") {
text1.innerHTML = "Please type in an ip address"
return;
} else {
text1.innerHTML = "";
}
if (result) {
text1.innerHTML = "";
} else {
text1.innerHTML = "invalid Ip address";
$('.details').fadeOut(900);
return;
}
$('.details').fadeIn(900);
let ipAddressValue = document.querySelector('.ip-address-value');
let locationValue = document.querySelector('.location-value');
let timeZone = document.querySelector('.timezone-value');
let ispValue = document.querySelector('.isp-value');
let apiUrl = `https://geo.ipify.org/api/v2/country,city?apiKey=at_hPMyH2iS7EzySYqA7uNyA2wGFx9fp&ipAddress=${ipContent}`
showLoader()
fetch(apiUrl)
.then(res => res.json())
.then(data => {
hideLoader();
console.log(data);
ipAddressValue.innerText = data.ip;
locationValue.innerHTML= data.location.country + ", " + data.location.region + " " + data.location.postalCode;
timeZone.innerHTML = "UTC" + data.location.timezone;
ispValue.innerText = data.isp;
let lat = data.location.lat;
let long = data.location.lng;
map.setView([lat, long], 8);
marker.setLatLng([lat, long]);
marker.bindTooltip(data.location.country + ", " + data.location.region).openTooltip();
});
}
map.setView([0, 0], 1);
marker.setLatLng([0, 0]);
//automatically pin the location of the user
let pinLocationDiv = document.querySelector('.pin-location');
pinLocationDiv.addEventListener("click", pinLocation);
//A FUNCTION TO GET THE CURRENT LOCATION OF THE USER
function pinLocation() {
$('.details').fadeOut(900);
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
alert("Geolocation is not supported by this browser");
}
}
function showPosition(position) {
let latitude = position.coords.latitude
let longitude = position.coords.longitude;
console.log(position);
map.setView([latitude,longitude], 8);
marker.setLatLng([latitude, longitude]);
marker.bindTooltip("Your current Location").openTooltip();
navigator.geolocation.clearWatch
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("User denied request for geolocation");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable");
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occured");
break;
}
}