[英]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 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.