diff options
author | Struan Donald <struan@exo.org.uk> | 2014-10-03 15:07:08 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2015-08-28 14:27:15 +0100 |
commit | a69d425c0e5c004145ac1ab70e2f7f9fc329b54c (patch) | |
tree | 8eb7ff0ff7aaa0fce8bc7214ec6e15b694fddbdc /www/js/locate.js | |
parent | 8fd15b58733c51d7f001f9eac66b7d03830ec0b4 (diff) |
update Android to Cordova 3.6
Required due to security issue
Remove Android directory as no longer required, move src -> www to match
standard layout, update .gitignore to avoid including the standard
platform files, update README based on Steve's zurich one
Diffstat (limited to 'www/js/locate.js')
-rw-r--r-- | www/js/locate.js | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/www/js/locate.js b/www/js/locate.js new file mode 100644 index 0000000..9d631f6 --- /dev/null +++ b/www/js/locate.js @@ -0,0 +1,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, _); |