@@ -16,6 +16,7 @@ import {
16
16
setRTLTextPlugin ,
17
17
} from "maplibre-gl" ;
18
18
import type {
19
+ LngLatBoundsLike ,
19
20
MapGeoJSONFeature ,
20
21
MapTouchEvent ,
21
22
StyleSpecification ,
@@ -299,19 +300,17 @@ function MapLibreView(props: {
299
300
map . on ( "idle" , ( ) => {
300
301
setZoom ( map . getZoom ( ) ) ;
301
302
setError ( undefined ) ;
302
- memoizedArchive ( )
303
- ?. getMetadata ( )
304
- . then ( ( metadata ) => {
305
- if ( metadata ) {
306
- const m = metadata as {
307
- version ?: string ;
308
- "planetiler:osm:osmosisreplicationtime" ?: string ;
309
- } ;
310
- setTimelinessInfo (
311
- `tiles@${ m . version } ${ m [ "planetiler:osm:osmosisreplicationtime" ] ?. substr ( 0 , 10 ) } ` ,
312
- ) ;
313
- }
314
- } ) ;
303
+ archiveInfo ( ) . then ( ( i ) => {
304
+ if ( i ?. metadata ) {
305
+ const m = i . metadata as {
306
+ version ?: string ;
307
+ "planetiler:osm:osmosisreplicationtime" ?: string ;
308
+ } ;
309
+ setTimelinessInfo (
310
+ `tiles@${ m . version } ${ m [ "planetiler:osm:osmosisreplicationtime" ] ?. substr ( 0 , 10 ) } ` ,
311
+ ) ;
312
+ }
313
+ } ) ;
315
314
} ) ;
316
315
317
316
const showContextMenu = ( e : MapTouchEvent ) => {
@@ -365,34 +364,51 @@ function MapLibreView(props: {
365
364
} ) ;
366
365
367
366
// ensure the dropped archive is first added to the protocol
368
- const memoizedArchive = ( ) => {
367
+ const archiveInfo = async ( ) : Promise <
368
+ { metadata : unknown ; bounds : LngLatBoundsLike } | undefined
369
+ > => {
370
+ if ( ! props . droppedArchive && props . tiles ?. endsWith ( ".json" ) ) {
371
+ const resp = await fetch ( props . tiles ) ;
372
+ const tileJson = await resp . json ( ) ;
373
+ return {
374
+ metadata : tileJson ,
375
+ bounds : [
376
+ [ tileJson . bounds [ 0 ] , tileJson . bounds [ 1 ] ] ,
377
+ [ tileJson . bounds [ 2 ] , tileJson . bounds [ 3 ] ] ,
378
+ ] ,
379
+ } ;
380
+ }
381
+
369
382
const p = protocolRef ( ) ;
370
383
if ( p ) {
371
- if ( props . droppedArchive ) {
372
- p . add ( props . droppedArchive ) ;
373
- return props . droppedArchive ;
374
- }
375
- if ( props . tiles ) {
376
- let archive = p . tiles . get ( props . tiles ) ;
384
+ let archive = props . droppedArchive ;
385
+ if ( archive ) {
386
+ p . add ( archive ) ;
387
+ } else if ( props . tiles ) {
388
+ archive = p . tiles . get ( props . tiles ) ;
377
389
if ( ! archive ) {
378
390
archive = new PMTiles ( props . tiles ) ;
379
391
p . add ( archive ) ;
380
392
}
381
- return archive ;
393
+ }
394
+ if ( archive ) {
395
+ const metadata = await archive . getMetadata ( ) ;
396
+ const header = await archive . getHeader ( ) ;
397
+ return {
398
+ metadata : metadata ,
399
+ bounds : [
400
+ [ header . minLon , header . minLat ] ,
401
+ [ header . maxLon , header . maxLat ] ,
402
+ ] ,
403
+ } ;
382
404
}
383
405
}
384
406
} ;
385
407
386
408
const fit = async ( ) => {
387
- const header = await memoizedArchive ( ) ?. getHeader ( ) ;
388
- if ( header ) {
389
- mapRef ?. fitBounds (
390
- [
391
- [ header . minLon , header . minLat ] ,
392
- [ header . maxLon , header . maxLat ] ,
393
- ] ,
394
- { animate : false } ,
395
- ) ;
409
+ const bounds = ( await archiveInfo ( ) ) ?. bounds ;
410
+ if ( bounds ) {
411
+ mapRef ?. fitBounds ( bounds , { animate : false } ) ;
396
412
}
397
413
} ;
398
414
@@ -411,22 +427,20 @@ function MapLibreView(props: {
411
427
const styleMajorVersion = props . npmVersion
412
428
? + props . npmVersion . split ( "." ) [ 0 ]
413
429
: STYLE_MAJOR_VERSION ;
414
- memoizedArchive ( )
415
- ?. getMetadata ( )
416
- . then ( ( m ) => {
417
- if ( m instanceof Object && "version" in m ) {
418
- const tilesetVersion = + ( m . version as string ) . split ( "." ) [ 0 ] ;
419
- if (
420
- VERSION_COMPATIBILITY [ tilesetVersion ] . indexOf ( styleMajorVersion ) < 0
421
- ) {
422
- setMismatch (
423
- `style v${ styleMajorVersion } may not be compatible with tileset v${ tilesetVersion } . ` ,
424
- ) ;
425
- } else {
426
- setMismatch ( "" ) ;
427
- }
430
+ archiveInfo ( ) . then ( ( i ) => {
431
+ if ( i && i . metadata instanceof Object && "version" in i . metadata ) {
432
+ const tilesetVersion = + ( i . metadata . version as string ) . split ( "." ) [ 0 ] ;
433
+ if (
434
+ VERSION_COMPATIBILITY [ tilesetVersion ] . indexOf ( styleMajorVersion ) < 0
435
+ ) {
436
+ setMismatch (
437
+ `style v${ styleMajorVersion } may not be compatible with tileset v${ tilesetVersion } . ` ,
438
+ ) ;
439
+ } else {
440
+ setMismatch ( "" ) ;
428
441
}
429
- } ) ;
442
+ }
443
+ } ) ;
430
444
} ) ;
431
445
432
446
createEffect ( ( ) => {
0 commit comments