Skip to content

Commit

Permalink
used bbox to set default zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
martinapippi committed Aug 8, 2023
1 parent 1e29b3a commit 31c1284
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/components/AnimatedMapboxLayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

<script lang="ts">
import { Component, Inject, Prop, Vue, Watch } from 'vue-property-decorator'
import { ImageSource, ImageSourceRaw, LngLatBounds, Map, RasterLayer } from 'mapbox-gl'
import { ImageSource, ImageSourceRaw, LngLatBounds, Map, RasterLayer ,LngLatLike } from 'mapbox-gl'

Check warning on line 7 in src/components/AnimatedMapboxLayer.vue

View workflow job for this annotation

GitHub Actions / build (16)

'LngLatLike' is defined but never used

Check warning on line 7 in src/components/AnimatedMapboxLayer.vue

View workflow job for this annotation

GitHub Actions / build (18.3)

'LngLatLike' is defined but never used

Check warning on line 7 in src/components/AnimatedMapboxLayer.vue

View workflow job for this annotation

GitHub Actions / build

'LngLatLike' is defined but never used
import { point } from "@turf/helpers"
import { toMercator } from "@turf/projection"
function getFrameId (layerName: string, frame: number): string {
return `${layerName}-${frame}`
}
Expand All @@ -22,9 +21,21 @@ function getCoordsFromBounds(bounds: LngLatBounds) {
]
}
interface MapboxLayerOptions {
function isBoundsWithinBounds(innerBounds: LngLatBounds, outerBounds: LngLatBounds) {
const innerNorthEast = innerBounds.getNorthEast();
const innerSouthWest = innerBounds.getSouthWest();
const outerNorthEast = outerBounds.getNorthEast();
const outerSouthWest = outerBounds.getSouthWest();
const isLngWithin = innerSouthWest.lng >= outerSouthWest.lng && innerNorthEast.lng <= outerNorthEast.lng;
const isLatWithin = innerSouthWest.lat >= outerSouthWest.lat && innerNorthEast.lat <= outerNorthEast.lat;
return isLngWithin && isLatWithin;
}
export interface MapboxLayerOptions {
name: string;
time: Date;
bbox:number[];
}
function getMercatorBboxFromBounds(bounds: LngLatBounds): number[] {
Expand Down Expand Up @@ -77,7 +88,7 @@ export default class AnimatedMapboxLayer extends Vue {
}
updateSource() {
if (this.layer === null) return
if (this.layer === null || this.newLayerId ) return
const time = this.layer.time.toISOString()
const source = this.mapObject.getSource(this.newLayerId) as ImageSource
const bounds = this.mapObject.getBounds()
Expand All @@ -89,8 +100,26 @@ export default class AnimatedMapboxLayer extends Vue {
})
}
setDefaultZoom() {
if (this.layer === null || this.layer.bbox === undefined) return
if (this.mapObject && this.layer.bbox.length === 4) {
const bbox = this.layer.bbox
const currentBounds = this.mapObject.getBounds()
const bounds = new LngLatBounds(
[bbox[0], bbox[1]], // sw
[bbox[2], bbox[3]], // ne
)
if (isBoundsWithinBounds(currentBounds, bounds)) {
return
} else {
this.mapObject.fitBounds(bounds)
}
}
}
@Watch('layer')
onLayerChange (): void {
this.setDefaultZoom()
if (!this.isInitialized) return
if (this.layer === null) {
this.removeLayer();
Expand Down

0 comments on commit 31c1284

Please sign in to comment.