【问题标题】:I cannot get GPS coordinates我无法获取 GPS 坐标
【发布时间】:2012-12-17 15:26:01
【问题描述】:

我的问题是我无法使用 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 的 Emulator Control 中模拟位置来获取位置。所以我尝试在我的真实手机上进行测试。当我安装在手机上时,虽然我打开了 GPS 和 wifi,但它无法自动获取当前位置。

我尝试在我的真实手机上运行另一个谷歌地图应用程序,它运行良好。我不明白代码有什么问题...谁能帮助我?

LOGCAT 和调试

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> RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@4610b970] (Running)  

第 75 行给出错误。

【问题讨论】:

  • 你能发布你的 android-manifest.xml 吗?我想你忘了给它添加位置权限。
  • @Marcelo 只是好奇,我也认为权限是罪魁祸首。但根据文档 (developer.android.com/reference/android/location/…),如果“不存在合适的权限”,该方法会抛出 SecurityException。如果直接在事件线程中运行,这个异常不应该出现在 logcat 中甚至抛出 ANR 吗?
  • 我已经写了所有这些权限:
  • @MisterSmith 是的,它应该使用SecurityException 强制关闭应用程序,我错过了应用程序可以在模拟器上获取位置的部分,而问题仅在真实设备上。跨度>

标签: android google-maps geolocation gps location


【解决方案1】:

由于您还请求 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 是否在设备上工作。或者,您可以尝试在清单中包含上述权限,然后检查以确保您在 onLocationChanged() 方法中收到的位置来自 GPS_PROVIDER。

另外,请尝试删除要转换为字符串数组的第一行。而是直接给变量赋值:

double lat = location.getLatitude();
double lng = location.getLongitude();

【讨论】:

  • 谢谢,但仍然通过评论网络提供者它没有帮助。
  • 尝试取消注释该行并将粗略位置权限添加到您的清单中。完成此操作后,如果网络提供商运行成功,您应该会在地图上看到更新。如果这可行,那么问题很可能是您手机中的 GPS 硬件无法获取 GPS 定位。在您的设备上测试您和 Google 地图应用程序时,您是否在同一物理位置背靠背测试了这两个应用程序?如果您在不同的位置,建筑物和其他障碍物可能会阻止您获得 GPS 定位。
  • 当我从 DDMS 输入 gps lat 和 long 时,我的应用程序基本上会崩溃,它会在存在 lat 和 long 的行出现空值错误。
  • 能否提供 DDMS 的输出日志?
  • 另外,请尝试消除要转换为字符串数组的第一行。相反,直接为变量赋值 - double lat = location.getLatitude().
猜你喜欢
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多