0

myFn.getMyCurrentPosition(data, function(data){ //Async call, need to use data like a callback - this section doesnt work. How can I use the current position coords and use it in a different function like a successfull callback?

var myFn = {
    this.localVar : null, 
    mysuccess : function (position) {
        this.myLocalVar = position.coords.latitude + ','+ position.coords.longitude; 
        return this.myLocalVar; 
    }, 
    myerror : function (error) {     
        return null; 
    }, 
    getMyCurrentPosition : function() {
        if(navigator && navigator.geolocation) { 
            //WORKS - mysuccess sets data asyncrhonously. 
            return navigator.geolocation.getCurrentPosition(this.mysuccess, this.myerror);          
        }
    }, 
    myInitializer : function(){
        //Initialize map, marker etc. for google maps API
        myFn.getMyCurrentPosition( function(){ //Async call, need to use data like a callback 
            //This code never runs! 
            if(this.myLocalVar){
                //doSomethingAfterCall - using this.myLocalVar, map, etc.;

            }
        });
    }
}

Update: TRIED THIS:

var myFn = {
    mysuccess: function (position) {
        myFn.myInitializer(); 
    }, 
    myerror: function (error) { 
        myFn.myInitializer(); 
    }, 

     myInitializer : function(){
        //Initialize map, marker etc. for google maps API
    }, 
    onLoadSet : function(){
        navigator.geolocation.getCurrentPosition(this.mysuccess, this.myerror);
    }

} 

myFn.onLoadSet(); 

Get this error: Failed to execute 'getCurrentPosition' on 'Geolocation': The callback provided as parameter 1 is not a function.

Loser Coder
  • 2,338
  • 8
  • 42
  • 66

3 Answers3

2

When you call myFn.getMyCurrentPosition, you pass it two arguments (data and a function).

When you defined it:

function() {

You didn't tell it to accept any arguments (and you didn't use the arguments object either).

You need to actually use the arguments you pass to it if you want anything to be done with them.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you for helping. actually I dont need to pass anything, just use the returned position coordinates. I also have some local variables where the call getMyCurrentPosition is made, so I want to use them and the position coordinates. Is there a way to do that. So basically, I need only a callback after the position/data is set, i.e get the data returned, without passing anything.? – Loser Coder May 18 '16 at 20:01
  • var map =something; myFn.getMyCurrentPosition( function(data){ //data needs to be the position - lat/long... }); – Loser Coder May 18 '16 at 20:02
  • 2
    There are no returned coordinates. It's an async function. You can't return from an async function. That's why we pass callbacks. – Quentin May 18 '16 at 20:03
  • can you show me how to pass/use the callback, that is the section I'm struggling to work with. I have a bunch of functionality after I get the position, but it needs to run only if position is returned successfully. – Loser Coder May 18 '16 at 20:06
  • so in the sucess callback, I have to move whatever it is I'm trying to do on sucess? I can't really write it out the way I have? let me try that. – Loser Coder May 18 '16 at 20:18
1

Try something like the new Permission api
I have built a extended version/polyfill of it with support for requesting & querying the state of the geolocation all promisify: browser-su

/***************
 *    query    *
 ***************/
su.query({
    name: 'geolocation'
}).then(permission => {
    console.log(permission) // {state, onchange}
    console.log(permission.state) // granted, prompt or denied
})


/***************
 *   request   *
 ***************/
su.request({
    name: 'geolocation',
    timeout: 5000 // Optional
}).then(position => {
    console.log(position)
} err => {
    console.log(err.name, err.message)
    // RequestDeniedError       User blocked access to geolocation
    // RequestDismissedError    User dismissed access to geolocation
    // RequestTimeoutError      Timeout expired
    // RequestUnavailableError  Position is unavailable
    // RequestUnsupportedError  This client dose not seem to have geolocation support
})

Firefox will also soon have support for revoking permission

Endless
  • 34,080
  • 13
  • 108
  • 131
0

This is what worked. I tried the other approach but probably messed up something in my syntax. It was a couple of issues: The JS needs to reference variable myFn.method instead of this.method for the myInitializer call within success/error callbacks. Because locationSuccess/error are expected callbacks, this.locationSucess works in the callback for getcurrentPosition.

var myFn = {
    locationSuccess: function (position) {
        myFn.myInitializer(position.coords); 
    }, 
    locationError: function (error) {  
        myFn.myInitializer();  
    }, 

     myInitializer : function(data){
        //Initialize map, marker etc. for google maps API
       //if(data) { doSomething(); }
       //else { doSomethingElse(); }

    }, 
    onLoadSet : function(){
        navigator.geolocation.getCurrentPosition(this.locationSucess, this.locationError, {timeout:1000});
    }

};

myFn.onLoadSet(); 

Another issue is known: Add a timeout as third parameter for the

navigator.geolocation.getCurrentPosition(this.locationSuccess, this.locationError, {**timeout:1000**}); 
Loser Coder
  • 2,338
  • 8
  • 42
  • 66