【问题标题】:How to stop app loading before getcurrentposition has data?如何在 getcurrentposition 有数据之前停止应用加载?
【发布时间】:2016-05-26 12:21:53
【问题描述】:

我有一些代码可以检测位置并返回经度和纬度。它将这些值存储在一个变量中,然后将它们添加到一个 URL,然后用于打开一个 webview。我遇到的问题是应用程序需要几毫秒才能获得位置日期,然后当应用程序加载 web 视图时,坐标显示为“未定义”,因为尚未收到位置数据。如果我创建一个警报并使用坐标回显 URL,然后设置超时(1 毫秒有效),那么警报将打印正确的信息。

我尝试在创建 web 视图的按钮上设置超时,但这似乎不起作用。

我可以采取其他方法吗?谢谢

var coords;

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


setTimeout(function() {
 alert("http://mywebsite.com/api/return-latlong/"+coords);
}, 500);


// Keep track of selected button and our button views
var buttonIdx = 0,
buttons=[]; 

// Definition of buttons to generate
var buttonsToCreate = [
{icon: '/images/icon1.png', icon2: '/images/icon1.png', name: "Webview1", url: "http://http://mywebsite.com/test.php?"+coords},
{icon: '/images/icon1.png', icon2: '/images/icon1.png', name: "Webview2", url: "http://mywebsite.com/page1"},
{icon: '/images/icon1.png', icon2: '/images/icon1.png', name: "Webview3", url: "http://mywebsite.com/page2"},
];



// Open Window

$.index.open();

【问题讨论】:

    标签: javascript ios titanium appcelerator


    【解决方案1】:

    您可以在 getCurrentLocation 的回调方法中使用您的网站 url。因为除此之外,您无法预测获取当前位置需要多长时间。

    Titanium.Geolocation.getCurrentPosition(function(e) {
        if (e.error) { 
           alert('Error: ' + e.error); 
        } else { 
           coords = e.coords.latitude;
           alert("http://mywebsite.com/api/return-latlong/"+coords);
        } 
    });
    

    【讨论】:

    • 我明白了,谢谢。但是有没有办法在检测到位置之前保持应用程序的加载?
    • 是的,您可以使用“Titanium.UI.ActivityIndi​​cator”。欲了解更多信息Red this
    • 这似乎只是一个加载 gif,我可以添加到应用程序中?我想真正停止应用加载,直到它有位置。
    • 不,您不能使用 gif 进行加载。您可以使用活动指示器进行加载,只需在调用地理位置获取当前位置时开始,并在收到地理位置响应时隐藏活动指示器。
    • 据我所见,Titanium.UI.ActivityIndi​​cator 用于在加载某些代码时添加加载动画。这不是我想要实现的,我试图阻止代码加载,以便 getCurrentPosition 有时间找到位置。目前,在 getCurrentPosition 有时间获取位置之前,应用程序的其余部分正在加载。如有错误请指正,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-02-03
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多