Sorry for my english. I'm trying to get a single location from GPS to put on global variables latitude, longitude. GPS turns on, but the activity goes on before data is retrieved from GPS.
My needs in other words... method getCurrentLocation() must finish only if a location has been found and the longitude and latitude variables are filled, so I could use them in other method. I know... user has to wait... I will solve this forward showing something on screen. What should I do? Thank you
I think I'm skipping stop listening GPS at some place. Where is better?
Code follows:
//Method to retrieve coordinates
public void getCurrentLocation() {
//Use GPS if possible
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//assign Listener to GPS
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
Toast.makeText(this, LocationManager.GPS_PROVIDER, Toast.LENGTH_SHORT).show();
}
else if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){//toherwise, use NETWORK
//assign Listener to NETWORK
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
Toast.makeText(this, LocationManager.NETWORK_PROVIDER, Toast.LENGTH_SHORT).show();
}
}
//Class to store the location recived in two variables
final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
//coordinates storing
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
Toast.makeText(getApplicationContext(), latitude + longitude, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}