简体   繁体   English

如果位置管理器无法通过GPS_Provider获取位置,请切换到NETWORK_Provider

[英]If Location Manager cannot fetch location with GPS_Provider, switch to NETWORK_Provider

I would like to use GPS_Provider to be my primary method for fetching a Users current location, however, in some cases GPS will not work. 我想使用GPS_Provider作为获取用户当前位置的主要方法,但是在某些情况下GPS无法正常工作。 If my app can't grab the Users location with GPS, I would like to switch to NETWORK_PROVIDER. 如果我的应用程序无法使用GPS捕获用户位置,我想切换到NETWORK_PROVIDER。 How can I implement this? 我该如何实施?

lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

//setting up criteria (biased toward GPS)
Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
String providerName = lm.getBestProvider(criteria, true);

//requesting location updates (if app cant use GPS, switch to requestLocationUpdates.NETWORK_PROVIDER)
lm.requestLocationUpdates(providerName, 180000, 0, ll);

One solution is to asynchronously request updates for GPS and NETWORK. 一种解决方案是异步请求更新GPS和网络。 You would need two separate listeners for both GPS and NETWORK. 对于GPS和网络,您将需要两个单独的侦听器。 Whichever listener returns first you would stop getting updates for the other provider. 无论哪个监听器首先返回,您都将停止获取其他提供程序的更新。 (ie if you get a result from the network first, stop getting updates for network AND gps.) (即,如果您首先从网络获得结果,请停止获取网络和gps的更新。)

Let me know if you are confused or not. 让我知道您是否感到困惑。

May or may not be a good idea, 可能不是一个好主意,

What I did, I registered both Network and GPS providers... Once I start fetching the GPS co ordinates, then I will ingore the co ordinates being fetched network provider using location.getProvider() using a boolean variable. 我做了什么,我注册了网络和GPS提供程序...开始获取GPS坐标后,我将使用一个布尔变量使用location.getProvider()来获取要获取的网络提供程序的坐标。

For my implementation. 对于我的实现。 I first check for the gps location, if it comes back null. 我首先检查gps的位置,如果它返回null。 Then check for the network location. 然后检查网络位置。

Location locationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (locationGps != null) {
            //set location as gps
            hasFix = true;
        }

        else if (locationNetwork != null) {
            //set location as network
            hasFix = true;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用GPS_PROVIDER / NETWORK_PROVIDER获取用户位置 - Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent 使用GPS_PROVIDER和NETWORK_PROVIDER获取当前位置的问题 - Issue in getting Current Location with GPS_PROVIDER and NETWORK_PROVIDER 使用OnStatusChanged从GPS_PROVIDER切换到NETWORK_PROVIDER? - Using OnStatusChanged to switch to NETWORK_PROVIDER from GPS_PROVIDER? 如何添加仅使用GPS_PROVIDER作为提供者而不使用NETWORK_PROVIDER触发的位置接近警报 - How to add a location proximity alert that triggers only using GPS_PROVIDER as provider and not using NETWORK_PROVIDER 查找GPS_Provider和Network_Provider在同一时间获得的位置 - Finding the location obtained by GPS_Provider and Network_Provider at the same time stamp GPS_PROVIDER的位置 - Location with GPS_PROVIDER GPS定位仅在NETWORK_PROVIDER上有效? - GPS location working only on NETWORK_PROVIDER? getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL - getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER 如何提示用户启用GPS_PROVIDER和/或NETWORK_PROVIDER? - How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM