如何将参数传递给getCurrentPosition成功回调?

Ray*_*eng 10 javascript windows-phone-7

如何在调用时将一个或多个参数传递给成功回调navigator.geolocation.getcurrentPosition

我如何可以通过devicereadyfoundLocgetGeoLoc法?

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.');
    }
};
Run Code Online (Sandbox Code Playgroud)

小智 21

函数(位置){}中的地理定位回调包装如下.然后,您可以根据需要向实际回调函数添加任意数量的参数.

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) {},
};
Run Code Online (Sandbox Code Playgroud)

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