钛移动,无法获得功能之外的可验证值

发布于 2024-12-20 07:00:30 字数 1884 浏览 0 评论 0原文

我不确定我是否真的很愚蠢或者只是错过了一些东西,但基本上我正在尝试访问变量纬度并将其放入 params 变量中,以便我可以在发布请求中发送它,但它似乎不起作用,如果我在函数外部的参数之前警告变量纬度,我为其分配一个值,则警报将返回空白。我的代码如下所示:

var latitude;



    Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', function(e) {
                if (e.success) {
                    var user = eval('('+e.result+')');

                    var currentTime = new Date();
                    var hours = currentTime.getHours();
                    var minutes = currentTime.getMinutes();
                    var month = currentTime.getMonth() + 1;
                    var day = currentTime.getDate();
                    var year = currentTime.getFullYear();                                               

                    if (Ti.Geolocation.locationServicesEnabled) { 
                        Titanium.Geolocation.purpose = 'Get Current Location'; 
                        Titanium.Geolocation.getCurrentPosition(function(e) { 
                            if (e.error) { 
                                alert('Error: ' + e.error); 
                            } else { 
                                latitude = e.coords.latitude;
                                longitude = e.coords.longitude;
                                accuracy = e.coords.accuracy;
                            } 
                        }); 
                    } else { 
                        alert('Please enable location services'); 
                    }

                    alert(latitude);

                    var params = {
                        username: user.username,
                        gender: user.gender,    
                        lastOnline:day+"/"+month+"/"+year+" -  "+hours+":"+minutes,
                        latitude:latitude,
                        //longitude:longitude,
                        //accuracy:e.coords.accuracy,
                    };

I am not sure if I am being really stupid or just missed something, but basically I am trying to access the variable latitude and put it in the params variable so I can send it in a post request, however it doesn't seem to work, if I alert the variable latitude just before params outside the function I assign a value to it the alert returns blank. the my code looks like the following:

var latitude;



    Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', function(e) {
                if (e.success) {
                    var user = eval('('+e.result+')');

                    var currentTime = new Date();
                    var hours = currentTime.getHours();
                    var minutes = currentTime.getMinutes();
                    var month = currentTime.getMonth() + 1;
                    var day = currentTime.getDate();
                    var year = currentTime.getFullYear();                                               

                    if (Ti.Geolocation.locationServicesEnabled) { 
                        Titanium.Geolocation.purpose = 'Get Current Location'; 
                        Titanium.Geolocation.getCurrentPosition(function(e) { 
                            if (e.error) { 
                                alert('Error: ' + e.error); 
                            } else { 
                                latitude = e.coords.latitude;
                                longitude = e.coords.longitude;
                                accuracy = e.coords.accuracy;
                            } 
                        }); 
                    } else { 
                        alert('Please enable location services'); 
                    }

                    alert(latitude);

                    var params = {
                        username: user.username,
                        gender: user.gender,    
                        lastOnline:day+"/"+month+"/"+year+" -  "+hours+":"+minutes,
                        latitude:latitude,
                        //longitude:longitude,
                        //accuracy:e.coords.accuracy,
                    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-12-27 07:00:30

很确定这是标准的“期望异步函数同步运行”问题。虽然我不熟悉 titan-mobile,但我猜测 Titanium.Geolocation.getCurrentPosition 是一个异步函数 - 这意味着您指定的回调函数在下一次运行时不会运行语句 alert(latitude); 执行。

要解决此问题,您需要确保在回调函数中调用任何需要设置地理位置的内容,而不是在此之前:

Titanium.Geolocation.getCurrentPosition(function(e) { 
    if (e.error) { 
        alert('Error: ' + e.error); 
    } else { 

        var params = {
            username: user.username,
            gender: user.gender,    
            lastOnline: day+"/"+month+"/"+year+" -  "+hours+":"+minutes,
            latitude: e.coords.latitude,
            longitude: e.coords.longitude,
            accuracy: e.coords.accuracy
        };

        // now do something with params
        initializeStuff(params);
    } 
}); 

Pretty sure this is the standard "expecting an asynchronous function to behave synchronously" issue. While I'm not familiar with titanium-mobile, I'm guessing Titanium.Geolocation.getCurrentPosition is an asynchronous function - that means that the callback function you specify won't have run by the time the next statement, alert(latitude);, executes.

To fix this, you need to make sure that anything requiring the geolocation to be set is invoked in the callback function, not before:

Titanium.Geolocation.getCurrentPosition(function(e) { 
    if (e.error) { 
        alert('Error: ' + e.error); 
    } else { 

        var params = {
            username: user.username,
            gender: user.gender,    
            lastOnline: day+"/"+month+"/"+year+" -  "+hours+":"+minutes,
            latitude: e.coords.latitude,
            longitude: e.coords.longitude,
            accuracy: e.coords.accuracy
        };

        // now do something with params
        initializeStuff(params);
    } 
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文