钛移动,无法获得功能之外的可验证值
我不确定我是否真的很愚蠢或者只是错过了一些东西,但基本上我正在尝试访问变量纬度并将其放入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很确定这是标准的“期望异步函数同步运行”问题。虽然我不熟悉 titan-mobile,但我猜测
Titanium.Geolocation.getCurrentPosition
是一个异步函数 - 这意味着您指定的回调函数在下一次运行时不会运行语句alert(latitude);
执行。要解决此问题,您需要确保在回调函数中调用任何需要设置地理位置的内容,而不是在此之前:
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: