tags:

views:

326

answers:

3

Hi all,

I am about to build an app in Android that will act as a clock in card for employees on the road.

At teh start of a job the user will click a button that will record the GPS location and current time (thus verifying that he is where he should be at a given time) and again at the end of teh job he records the time and GPS location.

SO I reckon this will be easy enough, except that I cant find a way of pulling current location data, the nearest I can find is "onLocationChanged" which implies that I cannot get a stationary GPS read. I know it has to be possible to do this but cannot find a working example of how it would be achieved.

any ideas??

Thanks

+1  A: 

You should try to android.location.LocationManager.getLastKnownLocation (http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)).

And check the time of the returned Location using Location.getTime (utc time).

david
ok, and excuse my ignorance, but lastKnownLocation dosnt imply my current location at this time.. I want the app to go get my location as soon as I press the button, is it not possible that the last known location happened a half hour ago 2 miles back the road?
Kevin Bradshaw
then onLocationChanged is the best way to get the most up-to-date position.
david
But thats my point, what if I am stationary and want to get the position that I am currently at? Is there a way of initialising my location to , say , 0 0, which would then trigger a location change?
Kevin Bradshaw
A: 

Request location updates and create a LocationListener (see LocationManager.requestLocationUpdates). You will receive regular updates with information about accuracy.

Until you get your first update, you can (as posted) use LocationManager.getLastLocation() and check its timestamp and accuracy.

Word of warning - some devices scan take forever to get you an accurate reading, and it's completely random.

EboMike
ok, so I so something like this then.. click the start button and and get a timestamp, and basically keep polling the lastKnown loaction untill I get a location with a timestamp greater than my starting timestamp.. I must say, I cant believe there isnt a way of doing an in the spot check. The device I am using is pretty quick at getting my location when I use google maps or the navigator...
Kevin Bradshaw
Don't poll lastKnown! Look at last known once and request updates as shown above. If it changes, you'll be notified. If it doesn't change, then lastKnown won't change either!
EboMike
Ok, I really dont want to appear lazy, but I learn best by example, so on that note is there any way you (or anyone) can post some sample code?
Kevin Bradshaw
+1  A: 

Hi all,

after a bit of research this is what I came up with:

public class UseGps extends Activity
{

    Button gps_button;
    TextView gps_text;
    LocationManager mlocManager;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState)

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gps_button = (Button) findViewById(R.id.GPSButton);
        gps_text = (TextView) findViewById(R.id.GPSText);


        gps_button.setOnClickListener(new OnClickListener() {
            public void onClick(View viewParam) {
                gps_text.append("\n\nSearching for current location. Please hold...");
                gps_button.setEnabled(false);
                mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                LocationListener mlocListener = new MyLocationListener();
                mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
            }
        });
    }

    /* Class My Location Listener */
    public class MyLocationListener implements LocationListener
    {
        @Override
        public void onLocationChanged(Location loc)
        {

            double lon = loc.getLatitude();
            double lat = loc.getLongitude();
            gps_text.append("\nLongitude: "+lon+" - Latitude: "+lat);
            UseGps.this.mlocManager.removeUpdates(this); 
            gps_button.setEnabled(true);

        }

        @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

        }

    }
}

What this does is set up an activity with a button and textview

set a listener on the button which starts the location manager.

I have set up a class MyLocationListener which implements LocationListener

then I override the onLocationChanged() method, basically telling it that the first location it gets it appends to the textview and then it removes the location manager.

Thanks to those who helped and hope this is of use to someone else.

Kevin

Kevin Bradshaw