我的问题是我无法使用 LocationManager 类和 LocationListener 获取 GPS 坐标。这是我在 OnCreate() 中调用的代码:
locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
currentLocListener = new MyLocationListener();
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, currentLocListener);
我有一个 MyLocationListener 类,它实现了 LocationListener 类
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
String coordinates[] = {""+location.getLatitude(),""+location.getLongitude()};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
mc.animateTo(p);
mc.setZoom(19);
MyMapOverlays marker = new MyMapOverlays(p);
List<Overlay> listofOverLays = mapview.getOverlays();
listofOverLays.clear();
listofOverLays.add(marker);
mapview.invalidate();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS is disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS is Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}}
我可以通过模拟 DDMS 中模拟器控制中的位置来获取位置。所以我尝试在我的真机上进行测试。当我在手机上安装它时,尽管我打开了 GPS 和 wifi,但它无法自动获取当前位置。
我尝试在我的真实手机上运行另一个来自市场的谷歌地图应用程序,它运行良好。我不明白代码有什么问题...任何人都可以帮助我吗?
日志和调试
DalvikVM[localhost:8631]
Thread [<1> main] (Suspended (exception NullPointerException))
DashboardActivity$MyLocationListener.onLocationChanged(Location) line: 75
LocationManager$ListenerTransport._handleMessage(Message) line: 191
LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 124
LocationManager$ListenerTransport$1.handleMessage(Message) line: 140
LocationManager$ListenerTransport$1(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running)
Thread [<5> Binder Thread #1] (Running)
Thread [<7> MapService] (Running)
Thread [<8> TrafficService] (Running)
Daemon Thread [<10> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="227047447357475747754d50494750624d50450c435243414a470c4a5656520c4b4f524e0c414d4c4c0c565141414f0c614d4c4c724d4d4e605b704d575647" rel="noreferrer noopener nofollow">[email protected]</a>@4610b970] (Running)
第 75 行给出错误。
最佳答案
由于您还请求 NETWORK_PROVIDER 的更新,因此您还需要包含以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
我的猜测是:
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
...抛出 SecurityException,因此注册 GPS_PROVIDER 的以下行永远不会执行。
为了排除故障,我建议注释掉从 NETWORK_PROVIDER 请求更新的行,然后查看 GPS 是否在设备上工作。或者,您可以尝试在 list 中包含上述权限,然后检查以确保您在 onLocationChanged() 方法中收到的位置信息来自 GPS_PROVIDER。
另外,尝试删除要转换为字符串数组的第一行。相反,直接为变量赋值:
double lat = location.getLatitude();
double lng = location.getLongitude();
关于android - 我无法获取 GPS 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13916980/