1- 'use strict' ;
2-
31import { getFitboundsLonRange } from '../../lib/geo_location_utils' ;
42import type { MapLayout , ScattermapData } from '../../types/generated/schema' ;
53
@@ -30,6 +28,8 @@ interface FitBoundsTrace extends Pick<ScattermapData, 'subplot' | 'visible'> {
3028 */
3129export function getMapFitBounds ( fullData : FitBoundsTrace [ ] , subplotId : string ) : LonLatBox | null {
3230 const validLons : number [ ] = [ ] ;
31+ let minLon = Infinity ;
32+ let maxLon = - Infinity ;
3333 let minLat = Infinity ;
3434 let maxLat = - Infinity ;
3535
@@ -49,6 +49,10 @@ export function getMapFitBounds(fullData: FitBoundsTrace[], subplotId: string):
4949 const la = lat [ j ] ;
5050 if ( Number . isFinite ( lo ) && Number . isFinite ( la ) ) {
5151 validLons . push ( lo ) ;
52+ // Track longitude bounds
53+ if ( lo < minLon ) minLon = lo ;
54+ if ( lo > maxLon ) maxLon = lo ;
55+ // Track latitude bounds
5256 if ( la < minLat ) minLat = la ;
5357 if ( la > maxLat ) maxLat = la ;
5458 }
@@ -57,18 +61,14 @@ export function getMapFitBounds(fullData: FitBoundsTrace[], subplotId: string):
5761
5862 if ( ! validLons . length ) return null ;
5963
60- let west : number ;
61- let east : number ;
62- const lonRange = getFitboundsLonRange ( validLons ) ;
63- if ( lonRange ) {
64- west = lonRange [ 0 ] ;
65- east = lonRange [ 1 ] ;
66- } else {
67- west = Infinity ;
68- east = - Infinity ;
69- for ( const lon of validLons ) {
70- if ( lon < west ) west = lon ;
71- if ( lon > east ) east = lon ;
64+ let west = minLon ;
65+ let east = maxLon ;
66+ // Only handle antimeridian if it actually gets crossed
67+ if ( maxLon - minLon > 180 ) {
68+ const lonRange = getFitboundsLonRange ( validLons ) ;
69+ if ( lonRange ) {
70+ west = lonRange [ 0 ] ;
71+ east = lonRange [ 1 ] ;
7272 }
7373 }
7474
0 commit comments