如何获取当前的GPS位置?

5
我即将在Android上构建一个应用程序,作为员工外出打卡的时钟卡。在工作开始时,用户将点击一个按钮,记录GPS位置和当前时间(从而验证他在特定时间应该在哪里),并在工作结束时再次记录时间和GPS位置。 因此,我认为这将足够容易,除了我找不到一种获取当前位置数据的方法。我能找到的最接近的是onLocationChanged,这意味着我无法获得静止的GPS读数。我知道这是可以做到的,但找不到一个可行的例子来实现它。
3个回答

3

经过一番研究,我得出以下结论:

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
        }
    }
}

这段代码设置了一个带有按钮和文本视图的活动。在按钮上设置一个监听器,用于启动位置管理器。

我创建了一个名为MyLocationListener的类,该类实现了LocationListener接口,然后我重写了onLocationChanged()方法,基本上告诉它第一次获取到的位置将被附加到文本视图中,然后移除位置管理器。

感谢那些提供帮助的人,我希望这对其他人有所帮助。


1

1
好的,不好意思我可能比较无知,但是“lastKnownLocation”并不代表当前我的位置。我希望当我按下按钮时,应用程序可以立即获取我的位置,难道不可能上次已知位置是半小时前在路上2英里处吗? - Kevin Bradshaw
然后,onLocationChanged 是获取最新位置的最佳方式。 - david
但这正是我的观点,如果我静止不动,想要获取当前所在的位置怎么办?有没有一种方法可以将我的位置初始化为0 0,从而触发位置更改? - Kevin Bradshaw

0

请求位置更新并创建一个LocationListener(参见LocationManager.requestLocationUpdates)。您将定期收到有关准确度的信息。

在获得第一个更新之前,您可以(如发布的那样)使用LocationManager.getLastLocation()并检查其时间戳和准确性。

警告 - 一些设备可能需要很长时间才能为您提供准确的读数,并且这是完全随机的。


好的,那我就像这样做...点击开始按钮并获取一个时间戳,然后基本上不停地轮询lastKnown loaction直到我得到一个具有大于起始时间戳的位置...我必须说,我无法相信没有一种可以进行即时检查的方法。当我使用谷歌地图或导航器时,我使用的设备在获取我的位置方面非常快... - Kevin Bradshaw
不要频繁轮询 lastKnown!只需查看一次 lastKnown 并按上面所示请求更新。如果它发生变化,您将收到通知。如果它没有变化,那么 lastKnown 也不会改变! - EboMike
好的,我真的不想显得懒惰,但我学习最佳方式是通过例子,因此,有没有什么办法你(或任何人)可以发布一些示例代码? - Kevin Bradshaw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接