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

查看:92
本文介绍了如何找出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天全站免登陆