在android studio中计算用户的移动速度 [英] Calculate Moving Speed of USER in android studio

查看:41
本文介绍了在android studio中计算用户的移动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算用户移动的速度.所以我们需要两件东西来计算速度,即 GPS 和加速度计.

I need to calculate Speed with which the user is moving. So we need two things to calculate speed which are GPS and Accelerometer.

但两者都有其局限性.

  1. GPS 不是一直可用.虽然我得到当前用户的位置总是我只从网络提供商那里得到不是来自 GPS.

  1. GPS is not available all the time.While I getting the current location of user always I am getting from Network provider only and not from GPS.

加速度计不准确.

那么我应该采用哪种方法?

So which approach should I go with?

推荐答案

请尝试以下使用GPS获取移动速度的方法

Please try the below way of getting the speed of moving using the GPS

public class MainActivity extends AppCompatActivity {

    TextView textView;
    private FusedLocationProviderClient mFusedLocationClient;
    private double lat;
    private double lng;
    private static int UPDATE_INTERVAL = 1000;
    private static int FATEST_INTERVAL = 1000;
    private static int DISPLACEMENT = 0;
    private double speed = 0.0;
    double currentSpeed,kmphSpeed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        textView = findViewById(R.id.textView);
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!runtime_permissions()) {
            requestLocations();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //stop location updates when Activity is no longer active
        if (mFusedLocationClient != null) {
            mFusedLocationClient.removeLocationUpdates(mLocationCallback);
        }
    }

    @SuppressLint("MissingPermission")
    private void requestLocations() {
        LocationRequest mLocationRequest = new LocationRequest();
//        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FATEST_INTERVAL);
        mLocationRequest.setSmallestDisplacement(DISPLACEMENT);

        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
    }

    LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            List<Location> locationList = locationResult.getLocations();
            if (locationList.size() > 0) {
                //The last location in the list is the newest
                Location location = locationList.get(locationList.size() - 1);
                Location location2 = locationResult.getLastLocation();
                lat = location.getLatitude();
                lng = location.getLongitude();

                speed = location.getSpeed();
                currentSpeed = round(speed,3,BigDecimal.ROUND_HALF_UP);
                kmphSpeed = round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);

                //speed in km/h
//                speed = (int) ((location.getSpeed() * 3600) / 1000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("speed=== "+speed+"\ncurrentspeed==="+currentSpeed+"\nkmph speed === "+kmphSpeed);
                    }
                });
//                Log.i("SensorTestActivity","SPEEDDDDDspeed=== "+speed+"     ");
            }
        }
    };

    public static double round(double unrounded, int precision, int roundingMode) {
        BigDecimal bd = new BigDecimal(unrounded);
        BigDecimal rounded = bd.setScale(precision, roundingMode);
        return rounded.doubleValue();
    }

    private boolean runtime_permissions() {
        if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);

            return true;
        }
        return false;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                onResume();
            } else {
                runtime_permissions();
            }
        }
    }
}

这篇关于在android studio中计算用户的移动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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