• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps do not get the current location

kandroid

Lurker
hi...
I have been trying to get my current location but fail to get it.
I am using wifi. I tried
geo fix -77.036519 38.896143. but still its not working.
Here is my code:
Code:
public class whereAmI extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);
        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        updateWithNewLocation(location);
        
    }
    private void updateWithNewLocation(Location location) {
    	String latLongString;
    	TextView myLocationText;
    	myLocationText = (TextView)findViewById(R.id.myLocationText);
    	if (location != null) {
    	double lat = location.getLatitude();
    	double lng = location.getLongitude();
    	latLongString = "Lat:" + lat + "\nLong:" + lng;
    	} else {
    	latLongString = "No location found";
    	}
    	myLocationText.setText("Your Current Position is:\n" +
    	latLongString);
    }
}
I think its the problem with my emulator but not sure. Can anybody please help me to fix this thing..!!
 
It can take up to 90 seconds for a normal GPS to get its first fix, and around 10-15 seconds when using A-GPS. So the location you get just after you initiates the LocationManager will never be very precise.
A better way to handle it is by registering a LocationListener to the manager. This approach also enables you to get updates every time the location changes etc.

This works like that:

Code:
/**
* Starts the location service and registers listeners
*/
private void initLoactionManagement(){
	locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	locLis = new LocationListener() {
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		public void onProviderEnabled(String provider) {
		}
		public void onProviderDisabled(String provider) {
		}
		public void onLocationChanged(Location location) {
			updateLocation(location);
		}
	};
	locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locLis); //<-- From network?
	locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLis); //<-- GPS Only?
}
 
Hi
I am using a locationlistener, but still not getting my current position.
Here is my full code

public class whereAmI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);

criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);

updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);

}
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);


}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
}
 
First, what do you mean by "I am using wifi" in your original post?

Second, do you get any location at all? And how much off is it?

And third, are you testing this on an actual device or in the emulator?
 
Back
Top Bottom