0

マップ ビューを使用せずに GPS データを取得しようとしています。以下のコードは O'Rielly の Programming Android book からのもので、動作するはずですが動作しません。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

おそらく、権限を追加する必要があります

<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
    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

おそらく、携帯電話の設定で場所をオンにする必要があります。

設定 --> 位置情報とセキュリティ --> 現在地

また、エミュレータを使用している場合は、DDMS を介して手動で行わない限り、位置データは生成されないことに注意してください。

于 2012-05-24T20:58:33.080 に答える