summaryrefslogtreecommitdiffstats
path: root/main/survey/js/Maps/UserLocation.js
blob: 89620be2641dc07b308b45fa3584e52a2ab6d872 (plain) (blame)
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
// Author: Uroš Podkrižnik (17.12.2015)
// Tip vprasanja = 26 

// USER LOCATION
// Skripta, ki najde lokacijo uporabnika

/**
 * funkcija za zacetek procedure ya ugotavljanje lokacije userja
 * @param {type} spremenljivka - int - id spremenljivke
 * @returns {undefined}
 */
function userLocation(spremenljivka) {
    // ali browser podpira pridobivanje lokacije uporabnika
    if (navigator.geolocation) {
        
        var options = {
            enableHighAccuracy: true,
            timeout: 0,
            maximumAge: Infinity
        };

        navigator.geolocation.getCurrentPosition(
            function (position) {
                //pozicija v latitude in longitude, ki jo najde
                var pos = {
                    lat: position.coords.latitude,
                    lng: position.coords.longitude
                };
                
                //accuracy of position
                //console.log('More or less meters ' + position.coords.accuracy);

                //klice proceduro, kjer se naprej operira s koordinatami
                userLocationProceduraF(pos, spremenljivka);
            }, function (error) {
                var warning = $('#warning_geo_'+spremenljivka);

                if(error){
                    switch(error.code) {
                        case error.PERMISSION_DENIED:
                            warning.html(lang['srv_resevanje_user_denied_geo_map']);
                            break;
                        case error.POSITION_UNAVAILABLE:
                            warning.html(lang['srv_resevanje_position_unavailable_geo_map']);
                            break;
                        case error.TIMEOUT:
                            warning.html(lang['srv_resevanje_timeout_geo_map']);
                            break;
                        case error.UNKNOWN_ERROR:
                            warning.html(lang['srv_resevanje_unknown_error_geo_map']);
                            break;
                    }
                }
                else
                    warning.html(lang['srv_resevanje_browser_not_support_geo_map']);

                warning.show();
            }
        );
    } else {
        // Browser doesn't support Geolocation
        handleLocationError(false);
    }
}

/**
 * procedura, ki se izvede, ko API najde lokacijo userja
 * @param {type} pos - koordinate - objekt {lat: ???, lng: ???}
 * @param {type} spremenljivka - int - id spremenljivke
 * @returns {undefined}
 */
function userLocationProceduraF(pos, spremenljivka) {
    //za omejitev max markerjev
    if(ml_sprem.indexOf(spremenljivka) > -1 || max_mark[spremenljivka]-st_markerjev[spremenljivka] != 0){
        //pretvori iz koordinat v naslov in nastavi label v infowindow
        GeocodingF(pos, function (data) {
            //ce ne vrne null - je nasel naslov
            if (data != null) {
                //kreira marker na lokaciji
                for (key in usrLoc_sprem) {
                    //Param fromSearchBox is true because of possible duplicate at multilocation with userloction ON,
                    //if user goes to previous page. 
                    //In very rare occasions this may couse bad UX in case: 
                    //user has clicked on location with address 'Undefined road', then userlocation returns
                    //address as 'Undefined road' - in this case, marker will not be created from userlocation.
                    createMarker(usrLoc_sprem[key], data.formatted_address, pos, true);
                }
            } else {
                //odpre se okno, ce je prislo do napake - null - (mozen je tudi prekratek delay med geocoding requesti)
                alert(lang['srv_resevanje_alert_location_not_found_map']);
            }
        });
    }
}