How can I obtain the current GPS location?

醉酒当歌 提交于 2019-12-05 01:24:08

问题


I am about to build an app in Android that will act as a clock-in card for employees on 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.


回答1:


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.




回答2:


You should try to android.location.LocationManager.getLastKnownLocation

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




回答3:


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.



来源:https://stackoverflow.com/questions/3428188/how-can-i-obtain-the-current-gps-location

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!