Skip to content

Commit 8ef5072

Browse files
committed
hopefully better zoom, mini fixes
1 parent 55e48ce commit 8ef5072

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

src/bg/bg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function redirect(req) {
2323
if (req.originUrl && req.url.match(matcher)) {
2424
if (!disabledHosts.includes(getHostname(req.originUrl))) {
2525
return {
26-
redirectUrl: runtimeMapUrl + '?' + req.url.split('?')[1],
26+
redirectUrl: runtimeMapUrl + '?' + req.url.split('?').pop(),
2727
}
2828
}
2929
}

src/map/utils/parsePB.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function convertType(item) {
4747
val = tileTypes[tileIndex < tileTypes.length && tileIndex >= 0 ? tileIndex : 0]
4848
break
4949
case 'z': // base64 encoded coords
50-
val = Buffer.from(item, 'base64').toString('utf-8')
50+
val = Uint8Array.fromBase64(item).toString('utf-8')
5151
}
5252

5353
return [val, type === 'm']

src/map/utils/read.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parsePB, tileTypes } from './parsePB.js'
22
import { parseDMS } from './parseDMS.js'
3-
import { getMapZoom } from './zoom.js'
3+
import { altitude2zoom } from './zoom.js'
44

55
export const nominatimQ = 'https://nominatim.openstreetmap.org/search?limit=1&format=json&q='
66
const cidMatch = /^0x[\da-f]+:0x[\da-f]+$/i
@@ -41,13 +41,13 @@ export async function readPB(param) {
4141
let data = parsePB(param.split('!').slice(1))[0]
4242

4343
/** @type {number[]} */
44-
let mapArea = data[0][0]
44+
let mapArea = data[0][0].reverse()
4545
mapData.area = {
46-
lat: mapArea[2],
46+
lat: mapArea[0],
4747
lon: mapArea[1],
4848
}
4949

50-
mapData.zoom = getMapZoom(mapArea[0])
50+
mapData.zoom = altitude2zoom(mapArea[2])
5151

5252
/** @type {any[] | string} */
5353
let currMarkers = data[1]
@@ -98,6 +98,8 @@ export async function readQ(addr) {
9898

9999
/** @type {LatLon[]} */
100100
const json = await res.json()
101+
if (!json.length) return null
102+
101103
/** @type {LatLon} */
102104
const body = json[0]
103105

src/map/utils/zoom.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
const factor = 35200000
2-
const precision = 10
1+
const earthCircumference = 40_075_000.017 // in m
2+
const factor = 1.25
33

44
/**
55
* Converts *altitude over the map* to *zoom level of the map*
6-
* TODO: Should be rewritten!!!
6+
* https://stackoverflow.com/a/37142662
77
*
8-
* Reference: https://groups.google.com/g/google-earth-browser-plugin/c/eSL9GlAkWBk/m/T4mdToJz_FgJ
9-
* @param {number} alt Altitude as number
10-
* @returns {number} Zoom level between 0 and 19
8+
* @param {number} alt Altitude in m
9+
* @returns {number} Zoom level
1110
*/
12-
export function getMapZoom(alt) {
13-
let zoom = Math.log2(factor / alt) * 1.225
14-
zoom = Math.round((zoom + Number.EPSILON) * precision) / precision
15-
16-
if (zoom < 0) {
17-
zoom = 0
18-
} else if (zoom > 19) {
19-
zoom = 19
20-
}
21-
return zoom
11+
export function altitude2zoom(alt) {
12+
return (Math.log2(earthCircumference / alt) + 1) * factor
2213
}

0 commit comments

Comments
 (0)