forked from mlevans/leaflet-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-hash.mjs
202 lines (178 loc) · 4.74 KB
/
leaflet-hash.mjs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import L from 'leaflet'
/**
* Class representing the hash management for a Leaflet map.
*/
export class Hash {
/**
* Creates an instance of Hash.
* @param {L.Map} map - A Leaflet map instance.
*/
constructor (map) {
/**
* @type {number}
* @description Delay time in milliseconds for change handling.
*/
this.changeDefer = 100
if (map) {
this.init(map)
}
}
/**
* Returns the zoom lebel based on the altitude and latitude
* @param {number} zoom - The zoom level
* @param {number} altitude - The altitude
* @returns {number} The latitude
*/
static getZoom (altitude, latitude) {
return Math.log2(133876102434.048 * Math.cos((latitude * Math.PI) / 180) / (altitude * 256))
}
/**
* Returns the altitude based on the zoom level and latitude
* @param {number} zoom - The zoom level
* @param {number} latitude - The latitude
* @returns {number} The altitude
*/
static getAltitude (zoom, latitude) {
return 133876102434.048 * Math.cos((latitude * Math.PI) / 180) / (Math.pow(2, zoom) * 256)
}
/**
* Parses the hash string and returns map center and zoom level.
* @param {string} hash - The hash string from the URL.
* @returns {{center: L.LatLng, zoom: number} | false} The parsed center and zoom level or false if invalid.
*/
static parseHash (hash) {
if (hash.startsWith('#')) {
hash = hash.slice(1)
}
if (hash.startsWith('@')) {
hash = hash.slice(1)
}
const args = hash.split(',')
if (args.length === 3) {
const lat = parseFloat(args[0])
const lon = parseFloat(args[1])
const zoom = Hash.getZoom(parseInt(args[2], 10), lat)
if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
return false
} else {
return {
center: new L.LatLng(lat, lon),
zoom
}
}
} else {
return false
}
}
/**
* Formats the map's center and zoom level into a hash string.
* @param {L.Map} map - A Leaflet map instance.
* @returns {string} The formatted hash string.
*/
static formatHash (map) {
const center = map.getCenter()
const zoom = map.getZoom()
const precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2))
const altitude = Math.round(Hash.getAltitude(zoom, center.lat))
return `#@${center.lat.toFixed(precision)},${center.lng.toFixed(precision)},${altitude}m`
}
/**
* Initializes the hash management for the map.
* @param {L.Map} map - A Leaflet map instance.
*/
init (map) {
this.map = map
// Reset the hash
this.lastHash = null
this.onHashChange()
if (!this.isListening) {
this.startListening()
}
}
/**
* Removes the hash management from the map.
*/
removeFrom () {
if (this.changeTimeout) {
clearTimeout(this.changeTimeout)
}
if (this.isListening) {
this.stopListening()
}
this.map = null
}
/**
* Handles map move events.
*/
onMapMove = () => {
// Bail if we're moving the map (updating from a hash),
// or if the map is not yet loaded
if (this.movingMap || !this.map || !this.map._loaded) {
return
}
const hash = Hash.formatHash(this.map)
if (this.lastHash !== hash) {
window.history.replaceState(null, '', hash)
this.lastHash = hash
}
}
/**
* Updates the map view based on the current hash in the URL.
*/
update = () => {
const hash = window.location.hash
if (hash === this.lastHash) {
return
}
const parsed = Hash.parseHash(hash)
if (parsed) {
this.movingMap = true
if (this.map) {
this.map.setView(parsed.center, parsed.zoom)
}
this.movingMap = false
} else {
this.onMapMove()
}
}
/**
* Handles hash change events.
*/
onHashChange = () => {
// Throttle calls to update() so that they only happen every `changeDefer` ms
if (!this.changeTimeout) {
this.changeTimeout = setTimeout(() => {
this.update()
this.changeTimeout = null
}, this.changeDefer)
}
}
/**
* Starts listening for map move and hash change events.
*/
startListening () {
if (this.map) {
this.map.on('moveend', this.onMapMove)
}
window.addEventListener('hashchange', this.onHashChange)
this.isListening = true
}
/**
* Stops listening for map move and hash change events.
*/
stopListening () {
if (this.map) {
this.map.off('moveend', this.onMapMove)
}
window.removeEventListener('hashchange', this.onHashChange)
this.isListening = false
}
}
/**
* Creates a new Hash instance for the given map.
* @param {L.Map} map - A Leaflet map instance.
* @returns {Hash} The created Hash instance.
*/
export function createHash (map) {
return new Hash(map)
}