【问题标题】:Phonegap+jquery mobile getLocationphonegap+jquery 手机获取位置
【发布时间】:2012-09-06 06:17:19
【问题描述】:

嗨,基本上我有一个应用程序需要每 150 秒获取当前位置,我使用的是安卓 2.3 的中国平板电脑,它没有 gps,我只使用蜂窝网络。 我一直在在 deviceready 函数中使用这段代码来获取当前位置:

 setInterval(
        function(){
            if (navigator.network.connection.type != Connection.NONE && navigator.geolocation) {
                watchID = navigator.geolocation.getCurrentPosition(
                    function(position){
                        plat = position.coords.latitude;
                        plon = position.coords.longitude;
                        console.log('location success');

                        if(map != null){
                            map.setView({center: new Microsoft.Maps.Location(plat,plon) });
                            pintaxi.setLocation(new Microsoft.Maps.Location(plat,plon));
                        }
                        count++;
                        if(count == 4){
                            count = 0;
                            console.log('Send data');
                            $.post("http://m2media.mx/plataforma/setpos.php", {latitud: plat, longitud: plon, referencia: 'DEV-008'});
                        }
                   }, 
                   displayError,
                    { timeout: 30000 });
            }
            else{
                if(watchID != null) navigator.geolocation.clearWatch(watchID);
                console.log('No connection, fail');
            }
        }, 60000
    );

这很重要,这个应用程序的活动在启动时运行,所以我的启动接收器是这样的:

public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, SmarttaxiActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
}

问题是getCurrent位置总是给我抛出超时异常,起初当应用程序在那里启动时在启用3​​g之前有大约10到20秒的间隔,但即使在3g之后启用它仍然会抛出超时异常,在应用程序内我有其他使用互联网连接的功能,如 twitter 小部件,它们运行没有问题。

问题是,如果我在启动定位功能后手动运行应用程序,效果会很好。 我在 logcat 上没有看到任何异常,我已经尝试修复了三天,但仍然没有运气,我希望有人知道可能是什么问题,或者我使用的逻辑是否错误。

感谢您抽出宝贵时间。

【问题讨论】:

  • 我不太确定,但只需检查您的 cordova.js 是否在 index.html 的顶部。试试看..
  • 仍然没有运气,但谢谢您的提示!

标签: android jquery mobile cordova geolocation


【解决方案1】:

您是否尝试过 navigator.geolocation.watchPositionnavigator.geolocation.clearWatch..?

navigator.geolocation.watchPosition(success_callback_function, error_callback_function, position_options)

navigator.geolocation.clearWatch(watch_position_id)

好的,这里是例子

var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000});

function geo_success (position) {

        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        //--- your code -- 
}

function geo_error(err) {
         console.log(err);
}

为了明确 watchPosition...

navigator.geolocation.clearWatch(wpid);

【讨论】:

  • 我试过使用 watchposition 但我有同样的问题,可能只是网络问题,因为如果我打开 wifi,它就像一个魅力:(
猜你喜欢
  • 2012-11-13
  • 1970-01-01
  • 2017-04-16
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多