-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation.js
More file actions
58 lines (49 loc) · 1.56 KB
/
location.js
File metadata and controls
58 lines (49 loc) · 1.56 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
function getCoordintes() {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
var lat = crd.latitude.toString();
var lng = crd.longitude.toString();
var coordinates = [lat, lng];
console.log(`Latitude: ${lat}, Longitude: ${lng}`);
getCity(coordinates);
return;
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
navigator.geolocation.getCurrentPosition(success, error, options);
}
function getCity(coordinates) {
var xhr = new XMLHttpRequest();
var lat = coordinates[0];
var lng = coordinates[1];
xhr.open('GET', " https://us1.locationiq.com/v1/reverse.php?key=pk.1b3227ba0f772bfdf526d12d4b1c6762&lat=" + lat + "&lon=" + lng + "&format=json", true);
xhr.send();
xhr.onreadystatechange = processRequest;
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
var city = response.address.state_district;
cityNameFilter(city);
return;
}
}
}
function cityNameFilter(city)
{
let spaceIndex = city.indexOf(' ');
if (spaceIndex !== -1) {
Cname = city.substring(0, spaceIndex + 1);
} else {
Cname = city;
}
console.log(Cname);
return Cname;
}
getCoordintes();