Search code examples
javascripthtmlgeolocation

Additional parameters to callback function


I'm using getCurrentPosition from the HTML5 geolocation functions but need to pass more than just the position parameter to the sucess callback function. (the getCurrentPosition callback is only supplied with the position parameter by default)

How can I do this?

navigator.geolocation.getCurrentPosition(doOnSuccess, true);

function doOnSuccess(position, switchFlag) {
    // process the position based on the value of switchFlag
}

My actual code is a lot more complex than this and the doOnSuccess function is re-used a number of times elsewhere so using anonymous function is not really an option.


Solution

  • As per my comment above, the clue to the answer was actually in the question - you DO use an anonymous function and then use that to call the correct function with all the required parameters.

    navigator.geolocation.getCurrentPosition(function() {
        doOnSuccess(position,true);
    });
    
    function doOnSuccess(position, switchFlag) {
        // process the position based on the value of switchFlag
    }