计算行进距离(而不是两者之间的距离) [英] Calculate distance traveled (not distance between)

查看:63
本文介绍了计算行进距离(而不是两者之间的距离)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算乘车旅行的距离!没有距离,没有距离.如果我们通过Google提供的API计算距离,则距离可以完全不同. Google可以从一个点到另一点分配1公里,但汽车可以按照骑手想要的方式行驶800米.使用加速度计没有帮助.它适用于步行,但永远不会加快速度.

I need to calculate distance traveled by car! Not distance between, not distance to no. The distance can be fully different if we calculate it via Google's provided API. Google can give 1KM from one point to another but car can go 800 meters in the way the rider want. Using Accelerometer didn't help. It works for walking but never for faster speed.

关于如何获得汽车行驶距离的任何建议?

Any suggestions on how can I get the distance traveled by the car?

我尝试使用Google的Location API:distanceTo或distanceBetween根本不是一个选项.与IN REAL相比,它可以提供显着不同的结果.在真正的汽车中,它可以穿越非常短的地方并在800米内达到目标,而Google可以在两个位置之间给出1公里的距离.

I've tried using Location APIs of Google: distanceTo or distanceBetween is not an option at all. It can give significant different result than IN REAL. In real car can travel through very short places and reach goal in 800 meters whereas Google can give 1KM distance between locations.

下面是我的应用程序的代码.速度惊人地正确.

Below is the code for my application. The speed is amazingly correct.

class HomeScreen : AppCompatActivity(), GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, SensorEventListener {
    override fun onSensorChanged(event: SensorEvent?) {
        val sensor = event?.sensor
        val values = event?.values
        var value = -1

        if (values != null && values.size ?: 0 > 0) {
            value = values[0].toInt()
        }


        if (sensor != null &&
            sensor.type == Sensor.TYPE_STEP_DETECTOR
        ) {
            val finalSteps = getDistanceRun(steps)
            val finalStepsTruncated = String.format("%.2f", finalSteps)
            distanceTV.text = "$finalStepsTruncated"
            steps++
        }
    }

    override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

    }

    override fun onConnectionFailed(p0: ConnectionResult) {
        val failed = p0
    }

    @SuppressLint("MissingPermission")
    override fun onConnected(p0: Bundle?) {
        if (locationPermissionsGranted(this)) {
            fusedLocationClient?.requestLocationUpdates(locationRequest, object : LocationCallback() {
                override fun onLocationResult(p0: LocationResult?) {
                    val location = p0
                    val metersPerSecond: Float = location?.lastLocation?.speed ?: 0f
                    val speed = metersPerSecond * 3600 / 1000
                    speedTV.text = "${Math.round(speed)} KM/H"
                }
            }, null)

        } else {
            requestPermission(
                this, 0,
                Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
            )
        }
    }

    override fun onConnectionSuspended(p0: Int) {
        val suspended = p0
    }

    private var fusedLocationClient: FusedLocationProviderClient? = null
    private var mGoogleApiClient: GoogleApiClient? = null
    private lateinit var locationRequest: LocationRequest
    private var steps: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home_screen)
        locationRequest = LocationRequest()
        locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
        locationRequest.interval = 1000
        locationRequest.fastestInterval = 500

        if (PermissionManager.locationPermissionsGranted(this)) {
            mGoogleApiClient = GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build()

            mGoogleApiClient?.connect()
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
            createLocationRequest()
        } else {
            requestPermission(
                this, 0,
                Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION
            )
        }

        val sManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        val stepSensor = sManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
        sManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (PermissionManager.locationPermissionsGranted(this)) {
            mGoogleApiClient = GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build()
            mGoogleApiClient?.connect()
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
            createLocationRequest()
        }
    }

    protected fun createLocationRequest() {
        val builder = LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest)

        val client = LocationServices.getSettingsClient(this)
        val task = client.checkLocationSettings(builder.build())

        task.addOnSuccessListener(this) {

            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...
        }

        task.addOnFailureListener(this) { e ->

            if (e is ResolvableApiException) {
                // Location settings are not satisfied, but this can be fixed
                // by showing the user a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    e.startResolutionForResult(
                        this@HomeScreen,
                        0
                    )
                } catch (sendEx: IntentSender.SendIntentException) {
                    // Ignore the error.
                }

            }
        }
    }

    fun getDistanceRun(steps: Long): Float {
        return (steps * 78).toFloat() / 100000.toFloat()
    }
}

推荐答案

我已经实现了基于gps读数的里程表,并且凭借一秒钟的读数,我已经能够将里程表讲座的准确度控制在1%以下.而且在长达50-70km的距离上,我无法检测到与道路标记之间的距离超过50m(我什至能够检测到何时移动了标记).所使用的过程涉及对速度读数进行积分(速度由gps表示为向量,因此无需计算其模数)和设备给定时间戳的精确读数.切勿使用位置读数...,因为这些读数非常糟糕,足以使您的里程表偏离读数的20%以上.但是使用这些速度并将它们整合在一起,可以为您提供良好的低音带通滤波.

I've implemented an odometer based on gps readings and, with one second readings I have been able to get odometer lectures well below under 1% error in accuracy. And well over distances up to 50-70km I was unable to detect over 50m difference from the road marks (I was even capable to detect when a mark has been moved) The procedure used involved integrating the velocity readings (velocity is given by gps as a vector, so no need to compute it's modulus) and precise readings of the timestamps given by the device. Never use the position readings... as those are bad enough to make your odometer to depart over 20% in the readings. But using the velocities and integrating those gives you a good bass band pass filtering.

使用的GPS是具有NMEA.0183输出的标准OEM GPS.速度读数的分辨率值为0.1,因此我并不希望精度低于1%,目前的gps设备提供的速度读数的分辨率低于0.001.

GPS used was a standard OEM gps with NMEA.0183 output. The velocity readings were 0.1 value in resolution, and so I was not expecting under 1% in accuracy, present gps devices give velocity readings with under 0.001 resolution.

这篇关于计算行进距离(而不是两者之间的距离)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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