This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
171 lines (147 loc) · 4.46 KB
/
main.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import './style.css'
import 'leaflet/dist/leaflet.css'
import markerIcon from 'leaflet/dist/images/marker-icon.png'
import markerShadow from 'leaflet/dist/images/marker-shadow.png'
import markerIcon2x from 'leaflet/dist/images/marker-icon-2x.png'
import L from 'leaflet'
// Define the initial map coordinates and zoom level
const initialLat = 51.505
const initialLng = -0.09
const initialZoom = 13
// Define the map and add the tile layer
const map = L.map('map-container').setView([initialLat, initialLng], initialZoom)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 18
}).addTo(map)
// Re-define the default marker icon
delete L.Icon.Default.prototype._getIconUrl
L.Icon.Default.mergeOptions({
iconRetinaUrl: markerIcon2x,
iconUrl: markerIcon,
shadowUrl: markerShadow
})
// Define and add the marker
const marker = L.marker([initialLat, initialLng]).bindPopup('A simple <b>popup</b>.').addTo(map)
// Define and add the circle
const initialRadius = 1000
const circle = L.circle([initialLat, initialLng], {
color: 'teal',
fillOpacity: 0.15,
radius: initialRadius
}).addTo(map)
// Define the scale control
L.control.scale().addTo(map)
// Define the event handlers
const onRangeChange = ({ target }) => {
let radius = target.value
document.getElementById('ran_value').value = radius
const unit = getUnit()
switch (unit) {
case 'km':
radius = radius * 1000
break
case 'mi':
radius = radius * 1609.34
break
}
circle.setRadius(radius)
}
const onRangeInputChange = ({ target }) => {
let radius = target.value
document.getElementById('ran').value = radius
const unit = getUnit()
switch (unit) {
case 'km':
radius *= 1000
break
case 'mi':
radius *= 1609.34
break
}
circle.setRadius(radius)
}
const onLocalButtonClick = () => {
try {
navigator.geolocation.getCurrentPosition(position => {
const { latitude, longitude } = position.coords
setLat(latitude)
setLng(longitude)
})
} catch {
console.info('Geolocation is not supported by this browser.')
}
}
const onGoButtonClick = () => {
try {
setViewMarker(getLat(), getLng())
toggleCircleLatLang()
} catch (error) {
console.info(error)
emptyInputs()
}
}
const onDefButtonClick = () => {
try {
setViewMarker(initialLat, initialLng)
toggleCircleLatLang(initialLat, initialLng)
setLat(initialLat)
setLng(initialLng)
} catch (error) {
console.info(error)
emptyInputs()
}
}
const onUnitChange = ({ target }) => {
const unit = target.value
let radius = document.getElementById('ran').value
switch (unit) {
case 'km':
radius *= 1000
break
case 'mi':
radius *= 1609.34
break
}
circle.setRadius(radius)
}
// Define the utility functions
const emptyInputs = () => {
document.getElementById('lat').value = ''
document.getElementById('lng').value = ''
document.getElementById('ran').value = initialRadius
document.getElementById('ran_value').value = initialRadius
document.getElementById('ran_unit').value = 'm'
}
const getLat = () => {
const lat = parseFloat(document.getElementById('lat').value)
return isNaN(lat) ? initialLat : lat
}
const setLat = (lat = initialLat) => {
document.getElementById('lat').value = isNaN(lat) ? initialLat : lat
}
const getLng = () => {
const lng = parseFloat(document.getElementById('lng').value)
return isNaN(lng) ? initialLng : lng
}
const setLng = (lng = initialLng) => {
document.getElementById('lng').value = isNaN(lng) ? initialLng : lng
}
const getUnit = () => {
return document.getElementById('ran_unit').value ?? 'm' //m, km, mi
}
const toggleCircleLatLang = (latitude = getLat(), longitude = getLng()) => {
circle.setLatLng([latitude, longitude])
}
const setViewMarker = (latitude, longitude) => {
map.setView([latitude, longitude])
marker.setLatLng([latitude, longitude])
}
// Attach the event listeners
document.getElementById('ran').addEventListener('input', onRangeChange)
document.getElementById('ran_unit').addEventListener('change', onUnitChange)
document.getElementById('ran_value').addEventListener('input', onRangeInputChange)
document.getElementById('btn-local').addEventListener('click', onLocalButtonClick)
document.getElementById('btn-go').addEventListener('click', onGoButtonClick)
document.getElementById('btn-def').addEventListener('click', onDefButtonClick)