如何使用android获取高度值?

时间:2012-09-27 11:46:41

标签: android location

我使用下面的代码,我得到的纬度和经度值是正确的,但海拔值是0.请帮帮我

locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000,
            0, (LocationListener) this);

public void onLocationChanged(Location location) {
    double lat_chn = (double) (location.getLatitude());
    double lng_chn = (double) (location.getLongitude());
    Log.d("Update Latitude= ", location.getLatitude() + "");
    Log.d("Update Longitude= ", location.getLongitude() + "");
    mapView.getOverlays().add(myLocationOverlay);

    String Text ="Latitude="+location.getAltitude()+
        "\nLongitude ="+location.hasAltitude()+"\nAltitude="+getAltitude(lat_chn,lng_chn);
}

1 个答案:

答案 0 :(得分:6)

您是否也可以使用sensor manager进行检查,因为传感器管理器就是为此而设的。它有getAltitude(float p0,float p)函数,它以大气压和海平面压力计算海拔高度。

希望它有所帮助。

在代码级别,您可以执行以下给定的操作....

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class MyActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;
    private float altitude = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (sensor != null) 
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    @Override
    public void onSensorChanged(SensorEvent event) {
        float presure = event.values[0];
        altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, presure);

        System.out.println("altitude => " + altitude);
    }
}