如何找出Android中缺少GPS坐标的原因? [英] How to find out why GPS coordinates are missing in Android?

查看:16
本文介绍了如何找出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坐标的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆