diff --git a/src/map-view.ts b/src/map-view.ts index 5dedcf6..6fbd29a 100644 --- a/src/map-view.ts +++ b/src/map-view.ts @@ -22,6 +22,7 @@ import { rtlPluginCode } from './map/rtl-plugin-code'; interface MapConfig { coordinatesProp: BasesPropertyId | null; + zoomProp: BasesPropertyId | null; markerIconProp: BasesPropertyId | null; markerColorProp: BasesPropertyId | null; mapHeight: number; @@ -237,7 +238,6 @@ export class MapView extends BasesView { if (isRestoringState || this.pendingMapState) return; const hasConfiguredCenter = this.mapConfig.center[0] !== 0 || this.mapConfig.center[1] !== 0; - const hasConfiguredZoom = this.config.get('defaultZoom') && Number.isNumber(this.config.get('defaultZoom')); // Set center based on configuration if (hasConfiguredCenter) { @@ -250,15 +250,27 @@ export class MapView extends BasesView { } } - // Set zoom based on configuration - if (hasConfiguredZoom) { - this.map.setZoom(this.mapConfig.defaultZoom); // Use configured zoom - } - else { - const bounds = this.markerManager.getBounds(); - if (bounds) { - this.map.fitBounds(bounds, { padding: 20 }); // Fit all markers + const zoomStrategy = this.config.get('zoomStrategy'); + + // Set the zoom based on the selected strategy + switch (zoomStrategy) { + case 'bounds': { + const bounds = this.markerManager.getBounds(); + + if (bounds) { + this.map.fitBounds(bounds, { padding: 20 }); + } + + break; } + + case 'properties': + this.map.setZoom(this.markerManager.getWidestMarkerZoom() ?? DEFAULT_MAP_ZOOM); + break; + + default: + this.map.setZoom(this.mapConfig.defaultZoom); + break; } }); @@ -440,6 +452,7 @@ export class MapView extends BasesView { private loadConfig(currentTileSetId: string | null): MapConfig { // Load property configurations const coordinatesProp = this.config.getAsPropertyId('coordinates'); + const zoomProp = this.config.getAsPropertyId('zoom'); const markerIconProp = this.config.getAsPropertyId('markerIcon'); const markerColorProp = this.config.getAsPropertyId('markerColor'); @@ -491,6 +504,7 @@ export class MapView extends BasesView { return { coordinatesProp, + zoomProp, markerIconProp, markerColorProp, mapHeight, @@ -699,6 +713,25 @@ export class MapView extends BasesView { key: 'center', placeholder: '[latitude, longitude]', }, + { + displayName: 'Zoom strategy', + type: 'dropdown', + key: 'zoomStrategy', + default: '', + options: { + '': 'Use default zoom', + 'bounds': 'Use marker bounds', + 'properties': 'Use widest zoom defined by markers', + }, + }, + { + displayName: 'Zoom property', + type: 'property', + key: 'zoom', + filter: prop => !prop.startsWith('file.'), + placeholder: 'Property', + shouldHide: (config) => config.get('zoomStrategy') !== 'properties', + }, { displayName: 'Default zoom', type: 'slider', diff --git a/src/map/markers.ts b/src/map/markers.ts index 7007a5e..2df74ab 100644 --- a/src/map/markers.ts +++ b/src/map/markers.ts @@ -47,6 +47,16 @@ export class MarkerManager { return this.bounds; } + getWidestMarkerZoom(): number | undefined { + const validZoomLevels = this.markers + .map(marker => marker.zoom) + .filter((z): z is number => z !== undefined); + + if (validZoomLevels.length === 0) return undefined; + + return Math.min(...validZoomLevels); + } + clearLoadedIcons(): void { this.loadedIcons.clear(); } @@ -63,6 +73,7 @@ export class MarkerManager { if (!entry) continue; let coordinates: [number, number] | null = null; + let zoom: number | undefined = undefined; try { const value = entry.getValue(mapConfig.coordinatesProp); coordinates = coordinateFromValue(value); @@ -71,10 +82,19 @@ export class MarkerManager { console.error(`Error extracting coordinates for ${entry.file.name}:`, error); } + try { + const value = entry.getValue(mapConfig.zoomProp); + zoom = value !== undefined ? Number(value) : undefined; + } + catch (error) { + console.error(`Error extracting zoom for ${entry.file.name}:`, error); + } + if (coordinates) { validMarkers.push({ entry, coordinates, + zoom, }); } } diff --git a/src/map/types.ts b/src/map/types.ts index 958e1b7..71a6ecb 100644 --- a/src/map/types.ts +++ b/src/map/types.ts @@ -3,6 +3,7 @@ import { BasesEntry } from 'obsidian'; export interface MapMarker { entry: BasesEntry; coordinates: [number, number]; + zoom?: number; } export interface MapMarkerProperties {