我怎样才能获得当前GPS位置? [英] How can I obtain the current GPS location?

查看:219
本文介绍了我怎样才能获得当前GPS位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打造的Andr​​oid的应用程序,将作为一个时钟卡的员工在道路上。

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

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

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.

因此我认为这将是很容易的,但我找不到拉动当前位置数据的方式。我能找到的最近的是 onLocationChanged 这意味着我不能得到一个固定的GPS读取。我知道它有可能做到这一点,但无法找到如何将它实现了工作的例子。

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

此设置了一个按钮和TextView的活动。在其启动位置管理器按钮设置监听器。

This sets up an activity with a button and textview. Set a listener on the button which starts the location manager.

我已成立专班, MyLocationListener ,它实现 LocationListener的,然后我重写 onLocationChanged()的方法,基本上是告诉它,它得到它的第一个位置追加到TextView中,然后将其删除位置管理器。

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.

这篇关于我怎样才能获得当前GPS位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆