我无法获得GPS坐标

时间:2012-12-17 15:26:01

标签: android google-maps geolocation gps location

我的问题是我无法通过使用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);

我有一个实现类LocationListener

的MyLocationListener类
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&amp; DEBUG

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)  

LINE 75给出错误。

1 个答案:

答案 0 :(得分:2)

由于您还要求更新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。

另外,尝试删除转换为String数组的第一行。而是直接为变量赋值:

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