navigator.geolocation和回调回调

时间:2015-05-17 08:27:49

标签: jquery html5 geolocation jquery-callback

最初,我有一个有效的代码:

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"]);
    }

}

然后我想整合此页面的解决方案: http://jsfiddle.net/CvSW4/

现在我的代码看起来像这样,它不再起作用了(在回调函数中丢失了):

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"])
}

我收到错误:

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

该行是:

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

如何使回调工作?

1 个答案:

答案 0 :(得分:0)

我刚刚发现了如何让它发挥作用。

下面的代码使用他/她的IP地址(html5功能)跟踪用户的位置。它首先使用高精度5秒,然后在出现错误的情况下,以低精度尝试10秒钟。

如果出现错误,则回调会发出错误。如果地理位置有效,则会给出坐标。

function get_coords(callback) {

    //If HTML5 Geolocation Is Supported In This Browser
    if (navigator.geolocation)
    {
         //Use HTML5 Geolocation API To Get Current Position
         // try high accuracy location for 5 seconds
         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;
             window.console&&console.log('coords : latitude=' + lat + ", longitude=" + lng);
             callback(["coords", lat, lng]);
        },
        function(error)
        {
            if (error.code == error.TIMEOUT)
            {
                // Attempt to get GPS loc timed out after 5 seconds, 
                // try low accuracy location for 10 seconds
                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;
                    window.console&&console.log('coords : latitude=' + lat + ", longitude=" + lng);
                    callback(["coords", lat, lng]);
                },
                function(error)
                {
                    if (error.code == 1)
                    {
                        window.console&&console.log('low accuracy geoloc_deactivated');
                        callback(["geoloc_deactivated"]);
                    }
                    else if (error.code == 2)
                    {
                        window.console&&console.log('low accuracy position_unavailable');
                        callback(["position_unavailable"]);
                    }
                    else  if (error.code == 3)
                    {
                        window.console&&console.log("low accuracy timeout");
                        callback(["timeout"]);
                    }
                },
                {
                    maximumAge:600000,
                    timeout:10000,
                    enableHighAccuracy: false
                });
            }
            if (error.code == 1)
            {
                window.console&&console.log('high accuracy geoloc_deactivated');
                callback(["geoloc_deactivated"]);
            }
            else if (error.code == 2)
            {
                window.console&&console.log('high accuracy position_unavailable');
                callback(["position_unavailable"]);
            }

        },
        {
            maximumAge:600000,
            timeout:5000,
            enableHighAccuracy: true
        });
    }
    else
    {
        window.console&&console.log("geoloc_not_supported");
        callback(["geoloc_not_supported"]);
    }
}

如何使用代码:在您要跟踪用户的页面上,使用此javascript来电:

get_coords(function(callback)
{
    if (callback[0]=="coords")
    {
        user_coords=[callback[1],callback[2]];
        //do stuff
    }
    else if(callback[0]=="position_unavailable")
    {
        //do stuff
    }
     else if(callback[0]=="timeout")
    {
        //do stuff
    }
    else if(callback[0]=="geoloc_deactivated")
    {
        //do stuff
    }
    else if(callback[0]=="geoloc_not_supported")
    {
       //do stuff
    }
});