Reputation: 12695
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.
Upvotes: 1
Views: 353
Reputation: 2866
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
Upvotes: 0
Reputation: 21778
You can implement your location listener that remembers delivered locations, probably averaging them in some way.
Upvotes: 0
Reputation: 10640
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) {
}
}
Upvotes: 1