如何每隔 1 分钟获取当前位置?

How to fetch the current Location in every 1 min of interval?(如何每隔 1 分钟获取当前位置?)
本文介绍了如何每隔 1 分钟获取当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了演示来显示用户当前的经纬度.现在我可以看到当前位置的经纬度.但我想将其设置为每隔 1 分钟显示一次.代码如下:

I have implemented the Demo to show the current lat-long of the User. Now I am able to see the lat-long of the current Position. but I want to Set it to to be displayed in every 1 min of interval. The code is as below:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener lListener = new mylocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
    }

    private class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            Toast.makeText(MainActivity.this,
                "My current location:
Latitude:"+location.getLatitude() + "
Logitude:" + location.getLongitude(),Toast.LENGTH_LONG).show();
            }
        }

        @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

        }
    }
}

那么,我应该怎么做才能让它以每 1 分钟的间隔显示当前的经纬度?我想我必须使用线程来实现它.如何让它成为可能?

So, What should I have to do to make it to display the current lat-long in every 1 min of interval? I think I have to use the thread to implement it. How to make it possible?

推荐答案

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);

方法中的参数表示 - 提供者、时间、距离和位置侦听器.

The argument in the method signify - provider, time, distance and the location listener.

因此,要从 Location 监听器请求更新,您需要将第二个参数设置为 1000*60,因为时间是以毫秒为单位记录的.

Therefore to request an update from the Location listener you need to set the second parameter to 1000*60, because the time is recorded in milliseconds.

这篇关于如何每隔 1 分钟获取当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)