代码之家  ›  专栏  ›  技术社区  ›  Kevin Bradshaw

如何获取当前的GPS位置?

  •  5
  • Kevin Bradshaw  · 技术社区  · 14 年前

    我将要在Android中构建一个应用程序,它将作为路上员工的上班打卡。

    在作业开始时,用户将单击一个按钮,该按钮将记录GPS位置和当前时间(从而验证他在给定时间应该在哪里),并在作业结束时再次记录时间和GPS位置。

    所以我认为这很容易,除了我找不到一种提取当前位置数据的方法。我能找到的最近的是 onLocationChanged 这意味着我无法获得固定的GPS读数。我知道必须有可能做到这一点,但却找不到一个有效的例子来说明如何做到这一点。

    3 回复  |  直到 7 年前
        1
  •  3
  •   Peter Mortensen Len Greski    12 年前

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

    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() 方法,基本上告诉它,它获取的第一个位置附加到textView,然后移除位置管理器。

    感谢那些帮助过我的人,我希望这对其他人有用。

        2
  •  1
  •   Rahul Sharma Rashid Kurbanov    7 年前

    你应该试着 android.location.LocationManager.getLastKnownLocation

    检查返回位置的时间,使用 Location.getTime(utc time) .

        3
  •  0
  •   EboMike    14 年前

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

    在你得到第一次更新之前,你可以使用 LocationManager.getLastLocation() 并检查其时间戳和准确性。

    警告词-有些设备需要永远扫描才能获得准确的读数,而且完全是随机的。