aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/locate.js
blob: 9d631f6814b33ed7958496745097cca73cc88005 (plain)
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
123
124
125
(function(FMS, Backbone, _) {
    _.extend( FMS, {
        Locate: function() { return {
            locating: 0,
            updating: 0,

            lookup: function(q) {
                var that = this;
                if (!q) {
                    this.trigger('search_failed', { msg: FMS.strings.missing_location } );
                    return false;
                }

                var url = CONFIG.FMS_URL + '/ajax/lookup_location?term=' + q;

                var x = $.ajax( {
                    url: url,
                    dataType: 'json',
                    timeout: 30000,
                    success: function(data, status) {
                        if ( status == 'success' ) {
                            if ( data.latitude ) {
                                that.trigger('search_located', { coordinates: { latitude: data.latitude, longitude: data.longitude } } );
                            } else if ( data.suggestions ) {
                                that.trigger( 'search_failed', { suggestions: data.suggestions, locations: data.locations } );
                            } else {
                                that.trigger( 'search_failed', { msg: data.error } );
                            }
                        } else {
                            that.trigger( 'search_failed', { msg: FMS.strings.location_problem } );
                        }
                    },
                    error: function(data, status, errorThrown) {
                        that.trigger( 'search_failed', { msg: FMS.strings.location_problem } );
                    }
                } );
            },

            geolocate: function( minAccuracy, skipLocationCheck ) {
                this.locating = 1;

                var that = this;
                this.watch_id = navigator.geolocation.watchPosition(
                    function(location) {
                        if ( that.watch_id === undefined ) { FMS.printDebug( 'no watch id' ); return; }

                        if ( minAccuracy && location.coords.accuracy > minAccuracy ) {
                            that.trigger('gps_locating', location.coords.accuracy);
                        } else {
                            that.locating = 0;
                            navigator.geolocation.clearWatch( that.watch_id );
                            delete that.watch_id;

                            if ( skipLocationCheck ) {
                                that.trigger('gps_located', { coordinates: location.coords } );
                            } else {
                                that.check_location(location.coords, false);
                            }
                        }
                    },
                    function(err) {
                        if ( that.watch_id === undefined ) { return; }
                        that.locating = 0;
                        navigator.geolocation.clearWatch( that.watch_id );
                        delete that.watch_id;
                        var errorMsg = FMS.strings.geolocation_failed;

                        if ( err && err.code == PositionError.PERMISSION_DENIED ) {
                            errorMsg = FMS.strings.geolocation_denied;
                        }
                        that.trigger('gps_failed', { msg: errorMsg } );
                    },
                    { timeout: 20000, enableHighAccuracy: true }
                );
            },

            trackPosition: function() {
                this.updating = 1;
                var that = this;
                this.track_watch_id = navigator.geolocation.watchPosition(
                    function(location) {
                        that.trigger('gps_current_position', { coordinates: location.coords } );
                    },
                    function() {},
                    { timeout: 20000, enableHighAccuracy: true }
                );
            },

            stopTracking: function() {
                this.updating = 0;
                if ( this.track_watch_id ) {
                    navigator.geolocation.clearWatch( this.track_watch_id );
                    delete this.track_watch_id;
                }
            },

            check_location: function(coords, showSpinner) {
                if ( typeof( showSpinner ) === 'undefined' ) {
                    showSpinner = true;
                }
                var that = this;
                $.ajax( {
                    url: CONFIG.FMS_URL + 'report/new/ajax',
                    global: showSpinner,
                    dataType: 'json',
                    data: {
                        latitude: coords.latitude,
                        longitude: coords.longitude
                    },
                    timeout: 10000,
                    success: function(data) {
                        if (data.error) {
                            that.trigger('gps_failed', { msg: data.error } );
                            return;
                        }
                        that.trigger('gps_located', { coordinates: coords, details: data } );
                    },
                    error: function (data, status, errorThrown) {
                        that.trigger('gps_failed', { msg: FMS.strings.location_check_failed } );
                    }
                } );
            }
        }; }
    });
})(FMS, Backbone, _);