Reputation: 517
I am in Callback Hell, and I don't really get how I will get out of it. I've tried reading other topics that touch on the subject and I still don't grasp the concept.
I am working with the Google Maps API v3 and I am trying to return the users current position so I can keep passing it on to other methods.
I have managed to succeed by using the jQuery .done() out of context, it throws a warning but I manage to get my coordinate object.
If I call userPosition, how can I make that method wait for the findUser method before returning the variable userPosition?
Edit: Uploaded wrong code, and after edit, the indentation sucks. :)
userPosition: function(position)
{
if (typeof position === 'undefined')
{
//Call on findUser, when ready callback it self to set variables
}
else
{
//var userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//return userPosition;
}
},
findUser: function()
{
var self = this;
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(
function(position) { // If successful
self.userPosition(position);
},
function(e) { // If something went wrong
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[e.code]);
},
{ enableHighAccuracy: true, timeout: this.timeoutVal, maximumAge: 0 }
);
}
else
{
// Set flag
}
},
Upvotes: 0
Views: 189
Reputation: 63514
Here's how I've done it in this jsfiddle.
var thing = {
findUser: function (callback) {
var _this = this;
// use the getPos method to get the geolocation
this.getPos(function (position) {
var lat, lng, userPos;
lat = position.coords.latitude;
lng = position.coords.longitude;
// add the new lat lng to the userPosition variable
_this.userPosition = new google.maps.LatLng(lat, lng);
// invoke the callback
callback();
});
return this;
},
getPos: function (callback) {
var geo = navigator.geolocation;
if (geo) {
// get the geolocation and pass it back to findUser
geo.getCurrentPosition(callback);
}
return this;
}
}
// if userPosition doesn't exist then grab the geolocation
if (!thing.userPosition) {
thing.findUser(function () {
console.log(thing.userPosition);
});
} else {
console.log(thing.userPosition);
}
Upvotes: 2
Reputation: 757
var isUserLocationSet = false;
userPosition: function(position){
if(!isUserLocationSet)
{
settimeout(userPosition, 200);
return;
}
if (typeof position === 'undefined')
{
var userPosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
return userPosition;
}
console.log(typeof position);
},
findUser: function(){
//set isUserLocationSet to true on completion in this method
var self = this;
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(
function(position) { // If successful
self.userPosition(position);
},
function(e) { // If something went wrong
var errors = {
1: 'Permission denied',
2: 'Position unavailable',
3: 'Request timeout'
};
alert("Error: " + errors[e.code]);
},
{ enableHighAccuracy: true, timeout: this.timeoutVal, maximumAge: 0 }
);
}
else
{
// Set flag
}
}
Here you would check on the flag repetitively using settimeout and will keep calling the function until that flag is set.
Upvotes: 0