diff options
author | Struan Donald <struan@exo.org.uk> | 2013-03-07 12:44:25 +0000 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2013-03-07 12:44:25 +0000 |
commit | 978fbc9c33e19d99e87a0d5dc6bfe37352be624a (patch) | |
tree | b61920e31c1bde25d6987449f3ef3f54786fdedd /www/js | |
parent | 86969471d257b11baa933f10eac01c13ab740036 (diff) |
add in locate javascript object
Diffstat (limited to 'www/js')
-rw-r--r-- | www/js/locate.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/www/js/locate.js b/www/js/locate.js new file mode 100644 index 0000000..4122662 --- /dev/null +++ b/www/js/locate.js @@ -0,0 +1,89 @@ +var Locate = ( function() { return { + locating: 0, + + lookup: function(q) { + var that = this; + if (!q) { + this.trigger('failed', { msg: 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('located', { coordinates: { latitude: data.latitude, longitude: data.longitude } } ); + } else if ( data.suggestions ) { + that.trigger( 'failed', { locs: data.suggestions } ); + } else { + that.trigger( 'failed', { msg: data.error } ); + } + } else { + that.trigger( 'failed', { msg: STRINGS.location_problem } ); + } + }, + error: function(data, status, errorThrown) { + that.trigger( 'failed', { msg: STRINGS.location_problem } ); + } + } ); + }, + + geolocate: function( minAccuracy ) { + this.locating = 1; + + $('#ajaxOverlay').show(); + var that = this; + this.watch_id = navigator.geolocation.watchPosition( + function(location) { + if ( that.watch_id == undefined ) { console.log( 'no watch id' ); return; } + if ( minAccuracy && location.coords.accuracy > minAccuracy ) { + that.trigger('locating', location.coords.accuracy); + } else { + console.log( 'located with accuracy of ' + location.coords.accuracy ); + that.locating = 0; + navigator.geolocation.clearWatch( that.watch_id ); + + that.check_location(location.coords); + } + }, + function() { + if ( that.watch_id == undefined ) { return; } + that.locating = 0; + navigator.geolocation.clearWatch( that.watch_id ); + + $('#ajaxOverlay').hide(); + that.trigger('failed', { msg: STRINGS.geolocation_failed } ); + }, + { timeout: 7000, enableHighAccuracy: true } + ); + }, + + check_location: function(coords) { + var that = this; + $.ajax( { + url: CONFIG.FMS_URL + 'report/new/ajax', + dataType: 'json', + data: { + latitude: coords.latitude, + longitude: coords.longitude + }, + timeout: 10000, + success: function(data) { + if (data.error) { + that.trigger('failed', { msg: data.error } ); + return; + } + that.trigger('located', { coordinates: coords, details: data } ) + }, + error: function (data, status, errorThrown) { + that.trigger('failed', { msg: STRINGS.location_check_failed } ); + } + } ); + } + +}}); |