-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (135 loc) · 4.65 KB
/
script.js
File metadata and controls
156 lines (135 loc) · 4.65 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const LocationEl = document.querySelector(".location");
const RadiusEl = document.querySelector(".radius");
const RadiusUnitSwich = document.querySelector(".radius_btn");
const GenerateEl = document.querySelector(".generate");
const ButtonLess = document.querySelector(".btnLess");
const ButtonMore = document.querySelector(".btnMore");
const MoreEl = document.querySelector(".more");
const LessEl = document.querySelector(".less");
const DetailedElements = MoreEl.children;
const ApiKeyEl = document.querySelector(".api_key");
const ErrMsgEl = document.querySelector(".api_err");
const MapEl = document.querySelector(".osm_map");
const map = L.map(MapEl);
const markerLayer = L.layerGroup().addTo(map);
let detailed = false;
let km = true;
// Listeners
GenerateEl.addEventListener("click", GetCoordinates);
ButtonMore.addEventListener("click", () => {
MoreEl.style.display = "grid";
LessEl.style.display = "none";
detailed = true;
});
ButtonLess.addEventListener("click", () => {
MoreEl.style.display = "none";
LessEl.style.display = "grid";
detailed = false;
});
RadiusUnitSwich.addEventListener("click", () => {
km = !km;
km == true ? RadiusUnitSwich.innerText = "KM" : RadiusUnitSwich.innerText = "MI";
});
window.addEventListener("load", () => {
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
})
async function GetCoordinates() {
const key = ApiKeyEl.value;
if(!key) {
await error("Please provide the API key!");
return;
}
const loc = LocationEl.value;
let radius = 0;
km == true ? radius = RadiusEl.value : radius = RadiusEl.value * 1.609;
let coordinates = [];
// Getting geocoded coordinates
if (!detailed) {
coordinates = await QueryCoordinates(`https://geocode.maps.co/search?q=${encodeURIComponent(loc)}&format=geojson&limit=1&addressdetails=0&api_key=${key}`);
} else {
const params = new URLSearchParams();
for(let el of DetailedElements) {
if(el.value.trim() !== "") {
params.append(el.className, el.value);
}
}
params.append("format", "geojson");
params.append("limit", "1");
params.append("addressdetails", "0");
coordinates = await QueryCoordinates(`https://geocode.maps.co/search?${params.toString()}&api_key=${key}`);
}
// Sanity check
if (coordinates.length == 0) {
await error("Error fetching coordinates");
return;
}
else console.log(coordinates);
if (radius <= 0 || isNaN(radius)) {
await error("Invalid radius");
return;
}
// Converting km to degrees
const {dx, dy} = GetRandomPoint(radius);
const {degX, degY} = ToDegrees(dx, dy, coordinates[1]);
const newX = coordinates[0] + degX;
const newY = coordinates[1] + degY;
console.log(`Was: x = ${coordinates[0]}, y = ${coordinates[1]}`);
console.log(`Became: x = ${newX}, y = ${newY}`);
MapEl.style.display = "block";
map.invalidateSize();
addMap(newX, newY);
}
// Methods to generate the coordinates
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function error(message) {
alert(message);
ErrMsgEl.innerText = message;
ErrMsgEl.style.display = "block";
await sleep(3000);
ErrMsgEl.style.display = "none";
ErrMsgEl.innerText = "";
return;
}
function addMap(x, y) {
var target = L.latLng(y, x);
map.setView(target, 10);
markerLayer.clearLayers();
L.marker(target)
.addTo(markerLayer)
.bindPopup(`<button
onclick="location.href='https://www.google.com/maps/search/?api=1&query=${y}%2C${x}'" type="button">
Open in Google Maps
</button>`)
.openPopup();
}
async function QueryCoordinates(query) {
try {
const response = await fetch(query);
const data = await response.json();
if(data.features.length != 0) {
const coordinates = data.features[0].geometry.coordinates;
return coordinates;
} else {
return [];
}
} catch (err) {
console.log("Error fetching: ", err);
return [];
}
}
function GetRandomPoint(radius) {
let angle = Math.random()*Math.PI*2;
let dx = Math.cos(angle) * radius;
let dy = Math.sin(angle) * radius;
console.log(`dx = ${dx}, dy = ${dy}, radius = ${Math.sqrt(dx**2 + dy**2)}`);
return {"dx": dx, "dy": dy};
}
function ToDegrees(x, y, xpos) {
let degY = y/111.320;
let degX = x/(111.320*Math.cos((xpos*Math.PI)/180));
return {"degX": degX, "degY": degY};
}