navigator.geolocation and callback of callback

Initially, I had a code that worked:

function get_coords(callback) {

    //If HTML5 Geolocation Is Supported In This Browser
    if (navigator.geolocation)
    {
         //Use HTML5 Geolocation API To Get Current Position
         navigator.geolocation.getCurrentPosition(function(position)
         {
             //Get Latitude From Geolocation API
             var lat = position.coords.latitude;
             //Get Longitude From Geolocation API
             var lng = position.coords.longitude;
             callback(["coords", lat, lng]);
        },
        function()
        {
            callback(["geoloc_deactivated"]);
        });
    }
    else
    {
        callback(["geoloc_not_supported"]);
    }

}

Then I wanted to integrate the solution of this page:
http://jsfiddle.net/CvSW4/

And now my code looks like this and it doesn’t work anymore (lost in the callback functions):

function get_coords(callback)
{

    //If HTML5 Geolocation Is Supported In This Browser
    if (navigator.geolocation)
    {
         //~ //Use HTML5 Geolocation API To Get Current Position
         navigator.geolocation.getCurrentPosition(
            successCallback,
            errorCallback_highAccuracy,
            {maximumAge:600000, timeout:5000, enableHighAccuracy: true}
        ); 
    }
    else
    {
        callback(["geoloc_not_supported"]);
    }

}


function successCallback(position)
{
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    position(["coords", lat, lng]);
}


function errorCallback_highAccuracy(callback_high_accuracy)
{
    if (error.code == error.TIMEOUT)
    {
        // Attempt to get GPS loc timed out after 5 seconds, 
        // try low accuracy location
        navigator.geolocation.getCurrentPosition(
            successCallback, 
            errorCallback_lowAccuracy,
            {maximumAge:600000, timeout:10000, enableHighAccuracy: false});
        return;
    }

    if (error.code == 1)
        callback_high_accuracy(["perm_denied"])
    else if (error.code == 2)
        callback_high_accuracy(["pos_unavailable"])
}


function errorCallback_lowAccuracy(callback_low_accuracy)
{
    if (error.code == 1)
        callback_low_accuracy(["perm_denied"])
    else if (error.code == 2)
        callback_low_accuracy(["pos_unavailable"])
    else if (error.code == 3)
        callback_low_accuracy(["timeout"])
}

I get the error:

Uncaught TypeError: object is not a functioncampaigns.min.js:37 successCallback

The line is:

position(["coords", lat, lng]);

How to make the callback work?


Source: jquery

Leave a Reply