我使用下面的代码,我得到纬度和经度值是正确的但海拔值是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);
}
Run Code Online (Sandbox Code Playgroud)
您是否可以与传感器管理器核实,因为传感器管理器就是为此而设计的; 它有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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2803 次 |
| 最近记录: |