【问题标题】:How to find my current location (latitude + longitude) in every 5 second in Android?如何在Android中每5秒找到我当前的位置(纬度+经度)?
【发布时间】:2016-10-10 07:18:56
【问题描述】:

我尝试了许多代码 sn-ps 但所有示例都需要太多时间来提供纬度和经度。

我想每 5 秒获取一次当前纬度。

【问题讨论】:

  • google locationmanager .... 它可以选择定期获取更新
  • stackoverflow.com/questions/23685072/… 这可能会有所帮助
  • “花费太多时间”是什么意思?什么样的代码 sn-ps 不起作用?如果您尝试过任何正确的代码 sn-ps,那么每 5 秒获取一次 GPS 位置更新应该没有问题,但获取第一个可能需要一段时间。如果您使用任何时间间隔或距离阈值限制,那么当然只有在满足条件时才会更新。如果您使用的是基于网络的位置,除非您大量移动,否则它不太可能发生变化。

标签: android


【解决方案1】:

您可以使用this post 来实现您正在寻找的东西。看看 setInterval(5000) 会这样做

【讨论】:

    【解决方案2】:

    您可以检查并集成这个库,它是 google play 服务位置的包装器,它也处理权限。

    google location

    在这个库中你可以找到各种设置参数的方法

    【讨论】:

      【解决方案3】:

      您可以使用FusedLocationProviderAPI 让您的应用程序位置感知。 使用 Google Play 服务位置 API,您的应用可以请求用户设备的最后一个已知位置。在大多数情况下,您对用户的当前位置感兴趣,这通常相当于设备的最后一个已知位置。

      为了接收最准确的位置更新,您可以使用此解决方案。 https://developer.android.com/training/location/index.html

      【讨论】:

        【解决方案4】:

        您可以使用 LocationManager 创建每 5 秒广播一次您的纬度和经度的服务。

        public class MyLocationService extends Service {
        
        private LocationManager locationManager;
        private LocationListener locationListener;
        
        public final String APP_BROADCAST_RECEIVER = "LatLonReciever"; // You should define it somewhere else as a static. e.g within Const class 
        
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (locationListener == null ) {
                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        
                locationListener = new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        locationIsChanged(location);
                    }
        
                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                    }
        
                    @Override
                    public void onProviderEnabled(String provider) {
                    }
        
                    @Override
                    public void onProviderDisabled(String provider) {
                    }
                };
            }
        
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5000, locationListener); // here 5000 is 5 seconds
            return START_STICKY;
        }
        
        
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        
        private void locationIsChanged(Location location){
            Intent intent = new Intent(APP_BROADCAST_RECEIVER); 
            intent.putExtra("lat", location.getLatitude());
            intent.putExtra("lon", location.getLongitude());
            sendBroadcast(intent);
            }
        }
        

        另外,不要忘记将接收者注册到您想要接收结果的位置:

        public final String APP_BROADCAST_RECEIVER = "LatLonReciever"; 
        
        BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        // Here you do what you want with recieved intent that contains lat and lon
                    }
                };
        
        IntentFilter intentFilter = new IntentFilter(APP_BROADCAST_RECEIVER);
        registerReceiver(broadcastReceiver, intentFilter);
        

        【讨论】:

          猜你喜欢
          • 2013-07-06
          • 2011-01-16
          • 1970-01-01
          • 2011-08-31
          • 2017-05-09
          • 1970-01-01
          • 1970-01-01
          • 2013-07-05
          • 1970-01-01
          相关资源
          最近更新 更多