How to find out why GPS coordinates are missing in Android?(如何找出Android中缺少GPS坐标的原因?)
问题描述
我正在尝试使用 GPS 获取设备的位置信息,但由于某种原因,有时会丢失两个坐标之一.
I am trying to get a device's location information using GPS, but for some reason one of the two coordinates is missing sometimes.
这是代码:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Log.d("LOCATION1", "Longitude: " + longitude);
Log.d("LOCATION2", "Latitude: " + latitude);
有时我确实得到了两个坐标,但并非总是如此,这让我想到了某处的某种延迟.发生这种情况时,有没有办法找出为什么 GPS 坐标丢失?
Sometimes I do get both coordinates, but not always, which makes me think about some kind of delay somewhere. Is there a way to find out why a GPS coordinate is missing when this happens?
推荐答案
因为 GPS 并不总是开启.getLastKnownLocation 将返回一个位置如果它知道一个并且它不是太陈旧.由于没有其他人在使用 GPS,它不知道其中之一.如果您需要位置,请 requestLocationUpdates 或 requestSingleUpdate,这将打开 GPS 并获取新位置.
Because the GPS isn't always on. getLastKnownLocation will return a location if it knows one and if it isn't too stale. Since nothing else was using the GPS, it doesn't know one. If you need a location, either requestLocationUpdates or requestSingleUpdate, which will turn on the GPS and get a new location.
这篇关于如何找出Android中缺少GPS坐标的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何找出Android中缺少GPS坐标的原因?
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
