2013-02-06 36 views
8

如何传递一个或多个参数,以成功调用navigator.geolocation.getcurrentPosition当我打电话吗?如何将参数传递给getCurrentPosition成功回调?

如何将devicereadyfoundLoc改为getGeoLoc方法?

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.'); 
    } 
}; 

回答

14

裹在功能(位置){}如下地理位置回调。然后你可以添加尽可能多的参数,你想要的实际回调函数。

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) {}, 
}; 

希望这可以帮助任何人可能偶然发现这一点。

+0

我偶然发现了它,它肯定没有帮助。 +1为你:) –

相关问题