How can I obtain the current GPS location?
I am about to build an app in Android that will act as a clock-in card for employees o开发者_如何学Cn the road.
At the 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 the job he records the time and GPS location.
SO I reckon this will be easy enough, except that I can't 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.
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
}
}
}
This sets 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
, and 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 I hope this is of use to someone else.
You should try to android.location.LocationManager.getLastKnownLocation
And check the time of the returned Location using Location.getTime(utc time)
.
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.
精彩评论