Android:如何在不使用MapView的情况下获取GPS数据?

时间:2012-05-24 20:42:46

标签: android gps location

我试图在不使用地图视图的情况下获取GPS数据。我下面的代码来自O'Rielly的编程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);}

3 个答案:

答案 0 :(得分: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();

    }
}

答案 1 :(得分: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);
}

答案 2 :(得分:0)

也许你需要打开手机设置中的位置:

设置 - &gt;位置和安全 - &gt;我的位置

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