2013-02-06 30 views
8

Làm cách nào để chuyển một hoặc nhiều tham số cho một cuộc gọi thành công khi gọi navigator.geolocation.getcurrentPosition?Làm cách nào để truyền tham số đến thành công getCurrentPosition gọi lại?

Làm cách nào tôi có thể vượt qua deviceready từ phương thức foundLoc đến 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.'); 
    } 
}; 

Trả lời

14

Bó callback geolocation trong chức năng (vị trí) {} như sau. Sau đó, bạn có thể thêm nhiều đối số như bạn muốn thực hiện chức năng gọi lại của bạn.

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

Hy vọng điều này sẽ giúp bất kỳ ai khác có thể vấp ngã điều này.

+0

Tôi tình cờ gặp nó và nó chắc chắn đã giúp ích. +1 cho bạn :) –

Các vấn đề liên quan