Android的LocationListener的切换,从GPS到网 [英] Android LocationListener Switching from GPS to Network

查看:317
本文介绍了Android的LocationListener的切换,从GPS到网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务实现LocationListener的监听GPS和网络。

I have a Service implementing LocationListener listening for both GPS and Network.

应用程序依赖于一个固定位置的饲料,但似乎GPS时,也很难得到一个locationfix网络位置犯规步骤。

The application is dependant on a constant location-feed, but it seems when GPS has a hard time getting a locationfix network location doesnt step in.

manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 30,0, LocationReporterService.this);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60 * 2,0, LocationReporterService.this);

问题
我如何确保我总是得到一个位置?
我怎样才能确保,如果我没有得到一个GPS定位,我得到一个网络位置?
它是一个已知的bug?
我应该有2个服务,GPSLocationService和NetworkLocationsService?
有没有解决办法? :)

Questions
How do I make sure that I always get a location?
How can I make sure that if I dont get a GPS-location, I get a Network-location?
Is it a known bug?
Should I have 2 services, GPSLocationService and NetworkLocationsService?
Is there a solution to this? :)

推荐答案

我同意大部分来自AlexBottoni好回答的意见,但在某些方面,我不能suppot他。

I agree with most of comments from AlexBottoni good answer, although in some points I can't suppot him.

概述

首先,只是为了检查你正在做的是正确的......

First, just to check that you are doing it right...

您的设置相同的 LocationListener的这两个供应商。从你在哪里reciving位置indentify你需要测试这样的:

You setup the same LocationListener for both providers. To indentify from where you are reciving the location you need to test it like this:

public void onLocationChanged(Location fix) {
    if(fix.getProvider().equals(LocationManager.GPS_PROVIDER)){
        //here Gps
    } else if(fix.getProvider().equals(LocationManager.NETWORK_PROVIDER)){
        //here Network
    }

另外,你设置一个不同的采集频率。 GPS 将提供一个新的位置,每30秒和网​​络每2分钟。

Also, you setup a different acquisition frequency. Gps is providing a new location every 30 seconds and Network every 2 minutes.

由于您没有强制的最短距离,你应该得到一个新的位置从供应商中的每一个(只要他们能得到修复)与频率要求。如果您没有收到一个解决方法,就是因为他们没有能够获得之一。

As you didn't impose a minimum distance, you should receive a new Location from each one of the providers (as long as they can get a fix) with the frequency requested. If you don't receive a fix, is because they weren't able to acquire one.

此外,它可能需要一点时间,然后要求得到修复(主要是GPS),因为它可能需要一些时间来shyncronize与卫星和固定的位置。

Also, it may takes a little longer then requested to get the fix (mainly with Gps), because it may take some time to shyncronize with satellites and fix a location.

后备

有没有builted,在后备从一个供应商到另一个。他们是independet,由Alex的说。我用下面的方法来实现回退:

There is no builted-in fallback from one provider to the other. They are independet, as said by Alex. I'm using the following approach to implement fallback:

  • 注册全球定位系统监听器和启动一个定时器
  • 在每一个GPS定位,重新启动定时器
  • 如果定时器reachs年底,注册网络监听器(GPS监听器不断注册)
  • 如果新的GPS定位到来,注销网络监听器,重新启动定时器

preferable提供商

尽管 GPS 可能不可用everyhere,是迄今为止最precise然后网​​络。在我的家乡,我得到6米的精度与GPS和1公里,您的网络: - (

Although Gps may not be available everyhere, is far most precise then Network. In my town, I get 6 meters accuracy with GPS and 1 Km with Network :-(

两个服务

没关系,你注册的监听器,活动或服务,独立OT在一起,只要你要求他们的供应商可以得到一个解决方法,你会得到的位置(假定应用程序:-)没有bug)

Doesn't matter where you register the listener, activity or service, separate ot together, as long as you request them and the provider can get a fix, you will get the location (assuming no bugs in application :-))

最后说明

确保您拥有的权限需要(ACCESS_FINE_LOCATION,网络等)。

Ensure you have the permissions need (ACCESS_FINE_LOCATION, INTERNET, etc).

确保手机安装了网络位置启用(一般默认为禁用)

Ensure that phone setup have Network Location enabled (usually default is disable)

大多数GPS接收支持更新有关卫星位置,从而改善修复时间信息。您可以使用 GPS萨特斯从市场,做到这一点。

Most Gps receivers support updating information about satellite location, which improves fix time. You can use GPS Satus from the market, to do it.

问候。

这篇关于Android的LocationListener的切换,从GPS到网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆