简体   繁体   English

如何从GPS_PROVIDER自动更改为NETWORK_PROVIDER

[英]How to change automatically from GPS_PROVIDER to NETWORK_PROVIDER

Is there any Code I can use, so that I do not have to implement it on my own to change automatically from GPS_PROVIDER to NETWORK_PROVIDER ? 有没有我可以使用的代码,所以我不必自己实现它自动从GPS_PROVIDER更改为NETWORK_PROVIDER

actually I use this code: 实际上我使用这段代码:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

But I want to do the following: 但我想做以下事情:
Try to use GPS, as long as it is not available use the NETWORK_PROVIDER . 尝试使用GPS,只要它不可用使用NETWORK_PROVIDER But when GPS get available switch to it. 但是当GPS可用时切换到它。 When the GPS is lost switch back to the NETWORK_PROVIDER . 当GPS丢失时切换回NETWORK_PROVIDER

Location loc =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);    
    if(loc == null)
    {
        loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(loc != null)
        {
            //do stuff on network provider
        }

    } else {
        // do stuff on gps provider
    }

You should have a look at the new fused location provider in Google Play Services. 您应该查看Google Play服务中新的融合位置提供商。 It was presented at the Google IO 2013. 它在Google IO 2013上展示。

It removes the hassle of selecting a certain LocationProvider, provides better results and much more. 它消除了选择某个LocationProvider的麻烦,提供了更好的结果等等。

For an overview have a look here or watch the Video . 有关概述,请查看此处或观看视频 For fused location provider code example tune in at 16:00. 对于融合位置提供商代码示例,请在16:00收听。

I will suggest a better option, try use a Criteria to select the best Provider. 我会建议一个更好的选择,尝试使用Criteria来选择最好的Provider。 It will switch automatically switch for the best provider. 它将自动切换为最佳提供商。

String provider = lm.getBestProvider(criteria, true);

Get a look here for more details. 在这里查看更多详细信息。

Good luck 祝好运

use locationmanager.getProviders as mentioned here . 使用这里提到的locationmanager.getProviders。 this way each time your location listener will be entered, you'll be able to pick your location from available providers. 这样,每次输入您的位置监听器时,您都可以从可用的提供商处选择您的位置。

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

相关问题 使用OnStatusChanged从GPS_PROVIDER切换到NETWORK_PROVIDER? - Using OnStatusChanged to switch to NETWORK_PROVIDER from GPS_PROVIDER? 如何提示用户启用GPS_PROVIDER和/或NETWORK_PROVIDER? - How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER? 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent 如何检查Xamarin表单中的network_provider和gps_provider? - How to check for network_provider and gps_provider in Xamarin forms? 无法使用GPS_PROVIDER / NETWORK_PROVIDER获取用户位置 - Not getting user location using GPS_PROVIDER / NETWORK_PROVIDER getLastKnownLocation使用GPS_PROVIDER和NETWORK_PROVIDER返回NULL - getLastKnownLocation return NULL using GPS_PROVIDER and NETWORK_PROVIDER NETWORK_PROVIDER和GPS_PROVIDER在片段中返回null - NETWORK_PROVIDER and GPS_PROVIDER returns null in fragments 使用GPS_PROVIDER和NETWORK_PROVIDER获取当前位置的问题 - Issue in getting Current Location with GPS_PROVIDER and NETWORK_PROVIDER 在android中,GPS_PROVIDER有效,但NETWORK_PROVIDER不起作用 - in android, GPS_PROVIDER works, but NETWORK_PROVIDER does not Android:locationManager GPS_provider VS Network_provider - Android: locationManager GPS_provider VS Network_provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM