【发布时间】:2014-08-25 00:39:15
【问题描述】:
在我的活动中,我需要使用 gps 获取手机的当前位置。我使用了以下代码:
Location gpslocation;
LocationManager lmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
gpslocation= lmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//error in the next line as follows java.lang.nullpointerexception
gpslatitude=gpslocation.getLatitude();
gpslongitude=gpslocation.getLongitude();
我知道 getlastknownlocation 将获取上次修复 gps 的位置。在这种情况下 getlastknownlocation 返回一个空指针。这就是为什么出现异常并且我的应用程序强制关闭的原因。另外我知道还有另一种使用 onlocationchanged 获取 gps 坐标的方法。但要使其正常工作,设备必须处于移动状态一段距离。由于在我的应用程序中我想以固定方式获取 gps 坐标,有没有其他方法可以在 android 中使用 gps 获取 gps 坐标。
我不想使用网络提供商,因为它只会提供距离我当前位置的实际位置约 5 公里的地点坐标。所以我不鼓励在我的应用程序中使用网络位置。因为 5 公里的距离对我来说不是可接受的范围。有没有其他方法可以使用代码修复 gps 坐标。在那里,当使用 lastknownlocation 时,我会为我的应用程序获取解决方案。
【问题讨论】:
标签: android networking gps geolocation latitude-longitude