/ / jquery, utilisation différée avec géolocalisation en html5 - javascript, jquery, html5, différé

jquery, utilisation différée avec la géolocalisation en html5 - javascript, jquery, html5, différé

Je viens de recevoir de l'aide avec mon gestionnaire de géolocalisation ici sur le forum alors que je rencontrais un dilemme. Je viens de réaliser que la géo-localisation est asynchrone. où comme je veux le synchroniser pour une programmation plus facile ^^

Voici la réponse finale, grâce à making3: http://jsfiddle.net/x4Uf4/1/

GeoManager.prototype.init = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.updateLocation.bind(this));
} else {
console.log("Geolocation is not activated!");
}
};

GeoManager.prototype.updateLocation = function (position) {
this.pos.lat = position.coords.latitude;
this.pos.lng = position.coords.longitude;
};


var GM = new GeoManager();
GM.init();

J'ai essayé d'utiliser $ .Deferred () d'une manière ou d'une autre, mais cela a échoué. Des conseils? :)

Réponses:

0 pour la réponse № 1

Arun P Johny a répondu à cette question dans les commentaires.

GeoManager = function () {
this.pos = {
lat: 0,
lng: 0
};
console.log("Geo ok...");
};

GeoManager.prototype.init = function (callback) {
var self = this;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
self.updateLocation(position);
callback(position);
});
} else {
console.log("Geolocation is not activated!");
}
};

GeoManager.prototype.updateLocation = function (position) {
this.pos.lat = position.coords.latitude;
this.pos.lng = position.coords.longitude;

console.log(this.pos);
};

GeoManager.prototype.getLat = function () {
return this.pos.lat;
}
GeoManager.prototype.getLng = function () {
return this.pos.lng;
};

//returns an object
GeoManager.prototype.getPos = function () {
return this.pos;
};

var GM = new GeoManager();
GM.init(function () {
console.log("pos", GM.getPos())
});