如何在没有侦听器或 requestLocationUpdates 的情况下获取任何 GPS 坐标
我只需要来自任何提供商的最后已知 GPS 位置
无需通知任何提供商进行更新。
在不添加侦听器或 requestLocationUpdates 的情况下是否可以实现这一点?
问题是我只需要一个大概的位置而无需阻塞延迟
我从 Activity
onCreate
尝试此代码,但 providers.size()
始终是零
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"></uses-permission>
I only need the last known gps location from any provider
without telling any provider to update.
Is this possible without adding listeners or requestLocationUpdates?
The thing is i need only an approximate position without blocking delay
Im trying this code from an Activity
onCreate
but the providers.size()
is always zero
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null)
break;
}
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION"></uses-permission>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管您可以从位置管理器获取最后一个已知位置,但它可能已经过时,因此最好获取所有提供商列表并检查最新位置,以便更准确
Even though you can get the last known location from location manager its may be outdated so its better to get all providers list and check the most current location so that it can be more accurate
尝试此代码片段。
Try this code snippet.