File size: 4,082 Bytes
3ca9355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78c3991
 
3ca9355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78c3991
3ca9355
 
 
 
 
 
 
 
 
 
 
 
16bcbad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01836f0
 
 
 
16bcbad
 
 
 
 
 
 
3ca9355
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * Geolocation Service
 * Migrated from Angular geolocation.service.ts
 */

const geolocationService = {
    /**
     * Get current GPS coordinates
     */
    async getCurrentPosition() {
        return new Promise((resolve, reject) => {
            if (!navigator.geolocation) {
                reject(new Error('Geolocation no est谩 soportada por este navegador'));
                return;
            }

            navigator.geolocation.getCurrentPosition(
                (position) => {
                    resolve({
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                        accuracy: position.coords.accuracy,
                        timestamp: new Date(position.timestamp)
                    });
                },
                (error) => {
                    let errorMessage = 'Error obteniendo ubicaci贸n';

                    switch (error.code) {
                        case error.PERMISSION_DENIED:
                            errorMessage = 'Permiso de ubicaci贸n denegado';
                            break;
                        case error.POSITION_UNAVAILABLE:
                            errorMessage = 'Ubicaci贸n no disponible';
                            break;
                        case error.TIMEOUT:
                            errorMessage = 'Tiempo de espera agotado';
                            break;
                    }

                    reject(new Error(errorMessage));
                },
                {
                    enableHighAccuracy: true,
                    timeout: 60000, // 60 seconds for full hardware satellite lock
                    maximumAge: 0   // Force fresh location every time
                }
            );
        });
    },

    /**
     * Watch position for continuous tracking
     */
    watchPosition(onSuccess, onError) {
        if (!navigator.geolocation) {
            onError(new Error('Geolocation no est谩 soportada'));
            return null;
        }

        return navigator.geolocation.watchPosition(
            (position) => {
                onSuccess({
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                    accuracy: position.coords.accuracy,
                    timestamp: new Date(position.timestamp)
                });
            },
            (error) => {
                onError(error);
            },
            {
                enableHighAccuracy: true,
                timeout: 60000,
                maximumAge: 0
            }
        );
    },

    /**
     * Clear position watch
     */
    clearWatch(watchId) {
        if (watchId && navigator.geolocation) {
            navigator.geolocation.clearWatch(watchId);
        }
    },

    /**
     * Get address name from coordinates using OpenStreetMap Nominatim
     */
    async getAddress(latitude, longitude) {
        try {
            if (!navigator.onLine) return null;

            const response = await fetch(
                `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}&zoom=18&addressdetails=1`
            );

            if (!response.ok) throw new Error('Error al consultar el servicio de direcciones');

            const data = await response.json();

            // Try to get a meaningful name
            const address = data.address;
            if (!address) return data.display_name || null;

            // Enhanced Marine-aware priority: search for natural features first
            const place = address.natural || address.water || address.beach ||
                address.island || address.islet || address.sea ||
                address.suburb || address.neighbourhood || address.village ||
                address.town || address.city || address.county || address.state;

            return place ? `${place}, RD` : data.display_name;
        } catch (error) {
            console.warn('Geocoding error:', error);
            return null;
        }
    }
};