Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to get altitude in LocationManager. i am alwaya getting zero

I am trying to get an Current Altitude from location.getAltitude() method. but it always returns zero. what should i do to get current altitude value. but getting longitude and lattitude correctly. please advice me. i have searched in google and worked out some samples but all are returned zero only.

Code which i have used :

in on create i have called =getMyCurrentLocation(); this method.

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)

        {

        }

        }
like image 239
RAJESH Avatar asked Nov 05 '22 01:11

RAJESH


1 Answers

What method did u used to get the location? Maybe u could try http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String) instead, that worked for me.

If not, maybe try another approach, there are several ones: http://developer.android.com/guide/topics/location/index.html

BTW: could u give us some code? On what device are you testing?

like image 73
Thkru Avatar answered Nov 09 '22 03:11

Thkru