android从可用的最佳提供商那里获取位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2919358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:54:00  来源:igfitidea点击:

android get location from best provider available

androidlocationlocation-provider

提问by spagi

I have this code to get the best available provider

我有这个代码来获得最好的可用提供商

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

locationListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(criteria, true);
Location mostRecentLocation = lm.getLastKnownLocation(provider);
if(mostRecentLocation != null) {
    latid=mostRecentLocation.getLatitude();
    longid=mostRecentLocation.getLongitude();
}
lm.requestLocationUpdates(provider, 1, 0, locationListener);

and then the listener

然后是听者

private class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
  if (loc != null) {
    latid = loc.getLatitude();
    longid = loc.getLongitude();
    // if(loc.hasAccuracy()==true){
    accuracyd = loc.getAccuracy();
    String providershown = loc.getProvider();    
    accuracy.setText("Location Acquired. Accuracy:"
      + Double.toString(accuracyd) + "m\nProvider: "+providershown);
    accuracy.setBackgroundColor(Color.GREEN);
    // }
    userinfo=usernamevalue+"&"+Double.toString(latid)+"&"+Double.toString(longid);
    submituserlocation(userinfo);
   }
}

When I tested it to a device(htc magic) I found out that when gps is disabled it locks from the network immediately. When I enable the gps it doesnt take any data from the network and waits till it locks from the gps.
I would like to lock the position like the google maps that until they have a good gps signal they use the network to determine my location.
I though the best criteria would do that but what they do is pick a provider once.
Is there something wrong with my code or I have to do threads and timeouts etc to make it happen?

当我将它测试到设备(htc magic)时,我发现当 gps 被禁用时,它会立即从网络中锁定。当我启用 gps 时,它不会从网络获取任何数据并等待它从 gps 锁定。
我想像谷歌地图一样锁定位置,直到他们有良好的 GPS 信号,他们才能使用网络来确定我的位置。
我虽然最好的标准可以做到这一点,但他们所做的只是选择一次提供商。
我的代码是否有问题,或者我必须执行线程和超时等才能使其发生?

回答by capecrawler

Maybe you can try listening to both the network provider and gps provider for a certain amount of time and then check the results from the two. If you don't have results from the gps, use the network results instead. That's how I did it.

也许您可以尝试在一段时间内同时收听网络提供商和 GPS 提供商的声音,然后检查两者的结果。如果您没有来自 GPS 的结果,请改用网络结果。我就是这样做的。

回答by Filippo Mazza

Maybe you can use getBestProvider (Criteria criteria, boolean enabledOnly)of LocationManagerwith criteria as in this other thread. See the official documentation.

也许你可以使用getBestProvider (Criteria criteria, boolean enabledOnly)LocationManager与标准在此另一个线程。请参阅官方文档

Hope it helps

希望能帮助到你