Android – Problem in Getting the Current Location in Android

androidandroid-layoutandroid-mapsgps

I want to display latitude and longitude my current location..
For this rather than searching in Google, i searched in SO.

I just want to display my current location latitude and longitude.

See my code below :

public class LocationGetter extends Activity {

@Override

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

TextView tv = new TextView(this);

LocationManager mlocManager = 
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new LocationManagerHelper();

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100,mlocListener);

if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                + '\n');
        tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                + '\n');

        Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));

} else {
    tv.setText("GPS is not turned on...");
}

/** set the content view to the TextView */
setContentView(tv);

}

/* Class My Location Listener */

public static class LocationManagerHelper implements LocationListener {

    private static double latitude;
    private static double longitude;

    @Override
    public void onLocationChanged(Location loc) {
        latitude = loc.getLatitude();
        longitude = loc.getLongitude();
        Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
    }

    @Override
    public void onProviderDisabled(String provider) { }

    @Override
    public void onProviderEnabled(String provider) { }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public static double getLatitude() {
        return latitude;
    }

    public static double getLongitude() {
        return longitude;
    }

}

} 

I have also added permission in manifest file :

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

See the output i am getting :

enter image description here

Where i am going wrong ? I dont understand, its a simple code and why its not working ?

Best Solution

I just verified your code and it works with these changes, you just need to move the tv.append() calls to the onLocationChanged() method as that is called each time, if you don't use that for the CallBack, then u will get the first set values only and it's only executed once.

        public void onLocationChanged(Location loc) {
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            Log.i("MyLocation",Double.toString(latitude)+" "+Double.toString(longitude));
            tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
                    + '\n');
            tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
                    + '\n');

            Log.i("MyLocation1",Double.toString(LocationManagerHelper.getLatitude())+" "+Double.toString(LocationManagerHelper.getLongitude()));

        }

and I have used these permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />    
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />    

You can look at these and filter out the ones you won't be interested in. I have used a downloaded .gpx file from here

I've tested it on Android 2.2 though, Location Updates working