Question

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)

        {

        }

        }
Was it helpful?

Solution

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?

OTHER TIPS

Network location does not have altitude. GPS does, but if you always get 0, then probably your GPS hardware is broken. Turn off network location setting on your phone and try the Map app to see if you get any fix from GPS.

I WONDER why it not getting altitude value,your CODE IS CORRECT.TRY TO REMOVE THE networkprovider and test the code only in gps see is it getting any values,if you are facing any problem please let me know in the code where are you displaying altitude can you please highlight it.if you are using String Text = "Longitud = " + (loc.getLongitude()) +"Latitud = " + loc.getLatitude()+"Altitude "+gooalt+"feet"; for altitude its wrong try to use loc.getAltitude() in place of gooalt.. .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top