2013-02-06 14 views
8

Come posso passare uno o più parametri a una chiamata riuscita quando si chiama navigator.geolocation.getcurrentPosition?Come passare i parametri a getCurrentPosition con successo?

Come posso passare deviceready da foundLoc a getGeoLoc metodo?

var app = { 

    onDeviceReady: function() { 
     alert = window.alert || navigator.notification.alert; 

     app.getGeoLoc('deviceready'); 
    }, 

    getGeoLoc: function (id) { 
     navigator.geolocation.getCurrentPosition(this.foundLoc, this.noLoc, { timeout: 3 }); 
    }, 

    foundLoc: function (position) { 
     var parentElement = document.getElementById('deviceready'); 
     var lat = parentElement.querySelector('#lat'); 
     var long = parentElement.querySelector('#long'); 

     lat.innerHTML = position.coords.latitude; 
     long.innerHTML = position.coords.longitude; 
    }, 

    noLoc: function() { 
     alert('device has no GPS or access is denied.'); 
    } 
}; 

risposta

14

Avvolgere il callback geolocalizzazione in funzione (posizione) {} come segue. Quindi puoi aggiungere tutti gli argomenti che vuoi alla tua effettiva funzione di callback.

var app = { 

    getGeoLoc : function (id) { 

     var self = this; 

     navigator.geolocation.getCurrentPosition(function(position) { 

      var myVar1, myVar2, myVar3; // Define has many variables as you want here 

      // From here you can pass the position, as well as any other arguments 
      // you might need. 
      self.foundLoc(position, self, myVar1, myVar2, myVar3) 

     }, this.noloc, { timeout : 3}); 
    }, 

    foundLoc : function(position, self, myVar1, myVar2, myVar3) {}, 
}; 

Spero che questo aiuti chiunque altro che potrebbe inciampare su questo.

+0

Mi sono imbattuto in esso e certamente ha aiutato. +1 per te :) –

Problemi correlati