1

I have simple flow: when I click the Button I should get current Latitude/Longitude at that time only, I am using LocationListner for this but problem is that the method onLocationChange() is calling only when I change the location otherwise gives 0.00/0.00. what shoukd I do please help me.

Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
  • http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android – marcinj Oct 21 '12 at 06:10

3 Answers3

1
 LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  LocationListener mlocListener = new MyLocationListener();
 mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        mlocListener);

 public class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
    loc.getLatitude();
    loc.getLongitude();

    Geocoder gcd = new Geocoder(getApplicationContext(),
            Locale.getDefault());
    try {
        mAddresses = gcd.getFromLocation(loc.getLatitude(),
                loc.getLongitude(), 1);

    } catch (IOException e) {

    }

    String cityName = (mAddresses != null) ? mAddresses.get(0)
            .getLocality() : TimeZone.getDefault().getID();
    String countryName = (mAddresses != null) ? mAddresses.get(0)
            .getCountryName() : Locale.getDefault().getDisplayCountry()
            .toString();


    mCurrentSpeed.setText("Longitude"+loc.getLongitude()+" Latitude"+loc.getLatitude());
}

@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) {
}
}
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84
0

You can implement your location listener that remembers delivered locations, probably averaging them in some way.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
0

You cant really ask Android to provide you with the current GPS locations right away your best bet would be wait till it gets a location Changed Update or use the Last Known values !

You can use the last known location.

Have a look here:

What is the simplest and most robust way to get the user's current location on Android?

Hope this helps

Community
  • 1
  • 1
Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35