Client - fit map bounds

This commit is contained in:
Sam 2021-09-24 12:20:42 +02:00
parent 695fa0d0f1
commit 34c51c6ef0

View File

@ -4,7 +4,13 @@
class="leaflet-container" class="leaflet-container"
v-if="geoJson.jsonData && center && bounds.length === 2" v-if="geoJson.jsonData && center && bounds.length === 2"
> >
<LMap :zoom="options.zoom" :center="center" :bounds="bounds"> <LMap
:zoom="options.zoom"
:center="center"
:bounds="bounds"
ref="workoutMap"
@ready="fitBounds(bounds)"
>
<LTileLayer <LTileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
:bounds="bounds" :bounds="bounds"
@ -18,7 +24,7 @@
<script lang="ts"> <script lang="ts">
import { gpx } from '@tmcw/togeojson' import { gpx } from '@tmcw/togeojson'
import { LGeoJson, LMap, LTileLayer } from '@vue-leaflet/vue-leaflet' import { LGeoJson, LMap, LTileLayer } from '@vue-leaflet/vue-leaflet'
import { ComputedRef, PropType, computed, defineComponent } from 'vue' import { ComputedRef, PropType, computed, defineComponent, ref } from 'vue'
import { GeoJSONData } from '@/types/geojson' import { GeoJSONData } from '@/types/geojson'
import { IWorkoutState } from '@/types/workouts' import { IWorkoutState } from '@/types/workouts'
@ -36,6 +42,9 @@
}, },
}, },
setup(props) { setup(props) {
const workoutMap = ref<null | {
leafletObject: { fitBounds: (bounds: number[][]) => null }
}>(null)
function getGeoJson(gpxContent: string): GeoJSONData { function getGeoJson(gpxContent: string): GeoJSONData {
if (!gpxContent || gpxContent !== '') { if (!gpxContent || gpxContent !== '') {
try { try {
@ -56,6 +65,12 @@
(bounds.value[0][1] + bounds.value[1][1]) / 2, (bounds.value[0][1] + bounds.value[1][1]) / 2,
] ]
} }
function fitBounds(bounds: number[][]) {
if (workoutMap.value?.leafletObject) {
workoutMap.value?.leafletObject.fitBounds(bounds)
}
}
const bounds = computed(() => const bounds = computed(() =>
props.workout props.workout
? [ ? [
@ -80,6 +95,8 @@
: {} : {}
), ),
options: { zoom: 13 }, options: { zoom: 13 },
workoutMap,
fitBounds,
} }
}, },
}) })