Im developing an app based on geolocation, so its mandatory to get the position in the first place, even before the execution of the rest of the app. So, how can I convert this module in a SYNCHRONOUS way????
var geolocation = (function() {
'use strict';
var geoposition;
var options = {
maximumAge: 1000,
timeout: 15000,
enableHighAccuracy: false
};
function _onSuccess (position) {
console.log('DEVICE POSITION');
console.log('LAT: ' + position.coords.latitude + ' - LON: ' + position.coords.longitude);
geoposition = position
};
function _onError (error) {
console.log(error)
};
function _getLocation () {
navigator.geolocation.getCurrentPosition(
_onSuccess,
_onError,
options
);
}
return {
location: _getLocation
}
}());
Thank you very much!
Geolocation has to remain asynchronous, but you can achieve what you want by passing in a callback to your module's main function and calling it each in the success and error functions, after they have completed their processing:
var geolocation = (function() {
'use strict';
var geoposition;
var options = {
maximumAge: 1000,
timeout: 15000,
enableHighAccuracy: false
};
function _onSuccess (callback, position) {
console.log('DEVICE POSITION');
console.log('LAT: ' + position.coords.latitude + ' - LON: ' + position.coords.longitude);
geoposition = position
callback();
};
function _onError (callback, error) {
console.log(error)
callback();
};
function _getLocation (callback) {
navigator.geolocation.getCurrentPosition(
_onSuccess.bind(this, callback),
_onError.bind(this, callback),
options
);
}
return {
location: _getLocation
}
}());
geolocation.location(function () {
console.log('finished, loading app.');
});
Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With