After trying for long..am facing problem in getting a user's current GPS location fix.
So, I thought that if can we somehow programatically get a location fix using pre-installed Google maps app's Current Location
fixer?
If I am able to fetch the location co-ordinates in the background..is this possible?
So, is it possible to somehow fetch the current location co-ordinates by using an intent or broadcast receiver?(supposing that the maps app is broadcasting it.. ? )
Update #2 : Finally..I got it to work..
Update: My earlier code is here(PasteBin Link)
Any advice is welcome..
Thanks..
Ok codebreaker, here is how I implemented my location fetcher:
// the listener to listen to the locations
private LocationListener listener = null;
// a location manager
private LocationManager lm = null;
// locations instances to GPS and NETWORk
private Location myLocationGPS, myLocationNetwork;
// instantiates fields
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationNetwork = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
myLocationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
listener = new myLocationListener();
// the listener that gonna notify the activity about location changes
public class myLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// "location" is the RECEIVED locations and its here that you should proccess it
// check if the incoming position has been received from GPS or network
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.removeUpdates(this);
} else {
lm.removeUpdates(listener);
}
}
@Override
public void onProviderDisabled(String provider) {
lm.removeUpdates(this);
lm.removeUpdates(listener);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
As you can see, there's not so much trickys. At the time that you instantiates the listener, you should see the GPS icon on the top of your Android device. Also, whenever your position changes (i.e. as you walk with your device), the OnLocationChanged method will be called.
Also, it is interesting to say that if you want to just get you locations, there are several ways to do it, all of then with different speeds of return and different acurracies. Please, check also GoogleGLM (a request to http://www.google.com/glm/mmap, that returns a json encoded strign with your position) services, triangulations and location by network. In the above snippets I've showed how to get location by either GPS and network. Hope that it has been of some help... :)