0

我试图在不使用地图视图的情况下获取 GPS 数据。我下面的代码来自 O'Rielly 的 Programming Android 书,应该可以工作但不是。我使用的是 Android 2.2.1 手机,应用程序在启动后立即关闭。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tvLattitude = (TextView) findViewById(R.id.tvLattitude);
    tvLongitude = (TextView) findViewById(R.id.tvLongitude);

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    tvLattitude.setText(Double.toString(loc.getLatitude()));
    tvLongitude.setText(Double.toString(loc.getLongitude()));

    locListenD = new DispLocListener();
    lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);}

private class DispLocListener implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        tvLattitude.setText(Double.toString(location.getLatitude()));
        tvLongitude.setText(Double.toString(location.getLongitude()));}
    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub
}
    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub
        }}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    lm.removeUpdates(locListenD);}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);}
4

3 回答 3

0
    private LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateNearestLocation(location);
}

private boolean updateNearestLocation(Location lastKnownLocation) {

    String locationProvider = LocationManager.NETWORK_PROVIDER;
    LocationManager m_locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    try {
        m_locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    } catch (IllegalArgumentException iae) {
        Log.i(TAG, "Could not find a location provider");
        return false;
    }
    if (lastKnownLocation == null) {

        lastKnownLocation = m_locationManager
                .getLastKnownLocation(locationProvider);
    } else {

        m_locationManager.removeUpdates(locationListener);
    }

    if (lastKnownLocation != null) {
        lastKnownLocation.getLatitude();
    lastKnownLocation.getLongitude();

    }
}
于 2012-05-24T20:49:19.127 回答
0

也许您需要添加权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  

到你的清单?

好吧,我建立了一个类似的项目,发现您的代码存在问题:在 onCreate 中,您立即尝试从以前的位置设置位置(在第一次运行时显然不可用)。我删除了这些行,代码似乎运行良好

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  tvLattitude = (TextView) findViewById(R.id.tvLattitude);
  tvLongitude = (TextView) findViewById(R.id.tvLongitude);

  lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  locListenD = new DispLocListener();
  lm.requestLocationUpdates("gps", 30000L, 10.0f, locListenD);
}
于 2012-05-24T20:50:52.170 回答
0

也许您需要在手机设置中打开位置:

设置 --> 位置和安全 --> 我的位置

此外,如果您使用的是模拟器,请记住它不会生成位置数据,除非您通过 DDMS 手动执行。

于 2012-05-24T20:58:33.080 回答