Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to getCurrentPosition success call back?

How can I pass in one or more parameters to a success call back when calling navigator.geolocation.getcurrentPosition?

How can I pass deviceready from foundLoc to getGeoLoc method?

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.');
    }
};
like image 535
Ray Cheng Avatar asked Feb 06 '13 23:02

Ray Cheng


1 Answers

Wrap the geolocation callback in function(position) {} as follows. Then you can add as many arguments as you want to your actual callback function.

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

Hope this helps anyone else who might stumble upon this.

like image 105
Charles R. Portwood II Avatar answered Oct 24 '22 07:10

Charles R. Portwood II