如何找出Android中缺少GPS坐标的原因?

How to find out why GPS coordinates are missing in Android?(如何找出Android中缺少GPS坐标的原因?)
本文介绍了如何找出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坐标的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)