I have tried everything and can’t seem to figure out how to properly set the zoom levels differently on map initialization and load for different devices. The goal is to have the “United States” be the boundaries, which is working perfectly on Macbook Pros w/ level 5 zoom, but it doesn’t work on other devices.
When I load the app, this is what I see:
On Load
This is what I would love to see:
Different map zoom-in size on different devices
I attempted to do something with calculateInitialZoom but it didn’t work.
I tried a few different methods and it didn’t work so I have reverted the code back to a working state and this is what I have:
<script setup>
import { ref, provide, reactive, computed } from 'vue';
import MapComponent from './components/organisms/map/MapComponent.vue';
import SearchPill from './components/molecules/forms/SearchPill.vue';
import LoadingSpinner from './components/atoms/feedback/LoadingSpinner.vue';
import Toast from './components/atoms/feedback/Toast.vue';
import { usePOIs, poiLoadingState } from './composables/usePOIs';
import { useToast } from './composables/useToast';
import RecentRoutes from './components/organisms/map/RecentRoutes.vue';
// Initialize toast system
const { showToast } = useToast();
// Create a reactive map state
const mapState = reactive({
instance: null,
center: [-95.7129, 37.0902],
zoom: 5,
layers: [],
view: null,
error: null
});
// Initialize POIs composable
const pois = usePOIs(mapState);
// Use the global loading state directly
const isLoading = computed(() => poiLoadingState.value);
// Function to calculate appropriate zoom level based on screen size
const calculateInitialZoom = () => {
try {
const width = window.innerWidth;
const height = window.innerHeight;
// Base zoom level for a standard screen (1920x1080)
const baseZoom = 5;
// Adjust zoom based on screen size
// For larger screens, zoom out more
// For smaller screens, zoom in more
const widthFactor = Math.log2(width / 1920);
const heightFactor = Math.log2(height / 1080);
const averageFactor = (widthFactor + heightFactor) / 2;
// Round to nearest integer and ensure it's between 3 and 7
return Math.min(Math.max(Math.round(baseZoom - averageFactor), 3), 7);
} catch (error) {
showToast('Error calculating initial zoom level', 'error');
return 5; // Fallback to default zoom
}
};
// Update initial zoom based on screen size
mapState.zoom = calculateInitialZoom();
// Provide the map state to all components
provide('mapState', mapState);
// Reference to map component
const mapComponent = ref(null);
const searchPill = ref(null);
// Handle map initialization
const handleMapInit = (instance) => {
try {
console.log('Map initialized:', instance);
mapState.instance = instance;
mapState.layers = instance.getLayers().getArray();
mapState.view = instance.getView();
console.log('[handleMapInit]: Map initialized successfully.');
} catch (error) {
console.error('[handleMapInit]: Error initializing map:', error);
mapState.error = error;
}
};
// Handle route selection from RecentRoutes
const handleRouteSelect = (route) => {
try {
if (searchPill.value) {
searchPill.value.handleRouteSelect(route);
console.log('[handleRouteSelect]: Route selected successfully.');
} else {
console.log('[handleRouteSelect]: Search component not available.');
}
} catch (error) {
console.error('[handleRouteSelect]: Error selecting route: ', error);
}
};
</script>
<template>
<!-- Root element with full viewport height and width -->
<div class="min-h-screen w-full flex flex-col overflow-hidden">
<!-- Main container with relative positioning for absolute children -->
<div class="relative flex-1 w-full">
<MapComponent
ref="mapComponent"
:initial-center="mapState.center"
:initial-zoom="mapState.zoom"
@map-initialized="handleMapInit"
/>
<SearchPill ref="searchPill" />
<RecentRoutes @select-route="handleRouteSelect" />
<LoadingSpinner :is-loading="isLoading" />
<Toast />
</div>
</div>
</template>
All help is appreciated on some guidance forward.

