最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Make geolocation call synchronously in javascript - Stack Overflow

programmeradmin2浏览0评论

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!

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!

Share asked Mar 5, 2014 at 11:52 Raul VallespinRaul Vallespin 1,3232 gold badges11 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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 pleted 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

I think you should not try to make it synchronously. Just implement some kind of event system for geolocation getter. This way it work asynchronously but geolocation initializes your app or ponents which requires geolocation.

Here's a dead simple example of how this could work:

var callbacks = [];

var onGeolocationReady = function (callback) {
     callbacks.push(callback);
}

function _onSuccess (position) {
     // iterare through each callback and invoce them
}

// and making ponent
onGeolocationReady(function () {
    // some code here which requires geolocation
});

I hope this helps.

发布评论

评论列表(0)

  1. 暂无评论