我正在尝试从 location.getAltitude() 方法获取当前高度。但它总是返回零。我应该怎么做才能获得当前的高度值。但正确获取经度和纬度。请给我建议。我在谷歌搜索并制定了一些样本,但所有样本都只返回零。
我使用过的代码:
在创建时我调用了 =getMyCurrentLocation(); 这种方法。
void getMyCurrentLocation() {
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
try{gps_enabled=locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
try{network_enabled=locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
if(gps_enabled){
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if(network_enabled){
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
if(gps_enabled){
location=locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(network_enabled && location==null) {
location=locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location != null) {
MyLat = location.getLatitude();
MyLong = location.getLongitude();
Toast.makeText(this,"My Lat : " + MyLat + "\n" + "My Long : " + MyLong, Toast.LENGTH_SHORT).show();
} else {
}
System.out.println(" LATTITUDE " + MyLat);
System.out.println("Longitude " + MyLong);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
MyLat =loc.getLatitude();
MyLong= loc.getLongitude();
gooalt=getElevationFromGoogleMaps(MyLat,MyLong) ;
double alt=loc.getAltitude(); **==>>> This one returns ZERO**
String Text = "Longitud = " + (loc.getLongitude()) +"Latitud = " + loc.getLatitude()+"Altitude "+gooalt+"feet";
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}