Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions src/map-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
debounce,
Menu,
QueryController,
Value,
StringValue,
NullValue,
ViewOption,
} from 'obsidian';
Expand All @@ -17,7 +15,7 @@ import { BackgroundSwitcherControl } from './map/controls/background-switcher';
import { StyleManager } from './map/style';
import { PopupManager } from './map/popup';
import { MarkerManager } from './map/markers';
import { hasOwnProperty, coordinateFromValue } from './map/utils';
import { hasOwnProperty, parseLatLng } from './map/utils';
import { rtlPluginCode } from './map/rtl-plugin-code';

interface MapConfig {
Expand Down Expand Up @@ -532,33 +530,19 @@ export class MapView extends BasesView {
}

private getCenterFromConfig(): [number, number] {
let centerConfig: Value;
let centerConfig: unknown = null;

try {
centerConfig = this.config.getEvaluatedFormula(this, 'center');
} catch (error) {
// Formula evaluation failed (e.g., this.file is null when no active file)
// Fall back to raw config value
const centerConfigStr = this.config.get('center');
if (String.isString(centerConfigStr)) {
centerConfig = new StringValue(centerConfigStr);
}
else {
return DEFAULT_MAP_CENTER;
if (centerConfig === null || centerConfig === undefined || centerConfig === NullValue.value) {
// If a formula is not specified, then get the static value.
centerConfig = this.config.get('center');
}
} catch (error) {
return DEFAULT_MAP_CENTER;
}

// Support for legacy string format.
if (Value.equals(centerConfig, NullValue.value)) {
const centerConfigStr = this.config.get('center');
if (String.isString(centerConfigStr)) {
centerConfig = new StringValue(centerConfigStr);
}
else {
return DEFAULT_MAP_CENTER;
}
}
return coordinateFromValue(centerConfig) || DEFAULT_MAP_CENTER;
return parseLatLng(centerConfig) || DEFAULT_MAP_CENTER;
}

private getConfigSnapshot(): string {
Expand Down Expand Up @@ -695,9 +679,10 @@ export class MapView extends BasesView {

{
displayName: 'Center coordinates',
type: 'formula',
type: 'text',
key: 'center',
placeholder: '[latitude, longitude]',
allowFormula: true,
},
{
displayName: 'Default zoom',
Expand Down
4 changes: 2 additions & 2 deletions src/map/markers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App, BasesEntry, BasesPropertyId, Keymap, Menu, setIcon } from 'obsidian';
import { Map, LngLatBounds, GeoJSONSource, MapLayerMouseEvent } from 'maplibre-gl';
import { MapMarker, MapMarkerProperties } from './types';
import { coordinateFromValue } from './utils';
import { parseLatLng } from './utils';
import { PopupManager } from './popup';

export class MarkerManager {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class MarkerManager {
let coordinates: [number, number] | null = null;
try {
const value = entry.getValue(mapConfig.coordinatesProp);
coordinates = coordinateFromValue(value);
coordinates = parseLatLng(value);
}
catch (error) {
console.error(`Error extracting coordinates for ${entry.file.name}:`, error);
Expand Down
21 changes: 17 additions & 4 deletions src/map/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Value, NumberValue, StringValue, ListValue } from 'obsidian';
import { NumberValue, StringValue, ListValue } from 'obsidian';

/**
* Converts a Value to coordinate tuple [lat, lng]
* Converts a Value, string, or array to coordinate tuple [lat, lng]
*/
export function coordinateFromValue(value: Value | null): [number, number] | null {
export function parseLatLng(value: unknown | null): [number, number] | null {
let lat: number | null = null;
let lng: number | null = null;

Expand All @@ -23,6 +23,19 @@ export function coordinateFromValue(value: Value | null): [number, number] | nul
lng = parseCoordinate(parts[1].trim());
}
}
else if (String.isString(value)) {
const parts = value.trim().split(',');
if (parts.length >= 2) {
lat = parseCoordinate(parts[0].trim());
lng = parseCoordinate(parts[1].trim());
}
}
else if (Array.isArray(value)) {
if (value.length >= 2) {
lat = parseCoordinate(value[0]);
lng = parseCoordinate(value[1]);
}
}

if (lat && lng && verifyLatLng(lat, lng)) {
return [lat, lng];
Expand All @@ -39,7 +52,7 @@ export function verifyLatLng(lat: number, lng: number): boolean {
}

/**
* Parses a coordinate value from various formats
* Parses a single coordinate value from various formats
*/
export function parseCoordinate(value: unknown): number | null {
if (value instanceof NumberValue) {
Expand Down