Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin distanceTo方法不给出结果,始终返回0.0km_Kotlin_Location_Distance - Fatal编程技术网

Kotlin distanceTo方法不给出结果,始终返回0.0km

Kotlin distanceTo方法不给出结果,始终返回0.0km,kotlin,location,distance,Kotlin,Location,Distance,我想知道两个地点之间的距离, 但我总是得到0.0公里的成绩 我试过两个版本,但都给出了0.0公里的结果 带板条的版本 带位置的版本 此版本与位置相同 private fun onLocationchanged(location:Location):Float { if(mStartLocation == null) { mStartLocation = mCurrentLocation mEndLocation

我想知道两个地点之间的距离, 但我总是得到0.0公里的成绩

我试过两个版本,但都给出了0.0公里的结果

  • 带板条的版本

  • 带位置的版本

  • 此版本与位置相同

    private fun onLocationchanged(location:Location):Float {
    
            if(mStartLocation == null)
            {
                mStartLocation  = mCurrentLocation
                mEndLocation = mCurrentLocation
            }
            else {
                mEndLocation = mCurrentLocation
            }
            mCurrentLocation = location;
            val distance = mStartLocation.distanceTo(mEndLocation)
            tvDistanzKm.text = distance.toString()
            return distance
            //Testing...No Difference
            val currentLocation = mStartLocation.latitude
            tvStart.text = currentLocation.toString()
            val endLocation = mEndLocation.latitude
            tvEnd.text = endLocation.toString()
        }
    
    这个版本是与拉特朗

    fun getLaufDistanz():Float {
    
    if(mLatLngStart == null) {
        mLatLngStart = mLatLngCurrent
        mLatLngEnde = mLatLngCurrent
    }else{
        mLatLngEnde = mLatLngCurrent
    }
    val laufStartPunkt = Location("")
    laufStartPunkt.latitude = mLatLngCurrent.latitude
    laufStartPunkt.longitude = mLatLngCurrent.longitude
    
    val laufEndPunkt = Location("")
    laufEndPunkt.latitude = mLatLngCurrent.latitude
    laufEndPunkt.longitude = mLatLngCurrent.longitude
    
    val laufDistanzInMeter = laufStartPunkt.distanceTo(laufEndPunkt)
    tvDistanzKm.text = laufDistanzInMeter.toString()
    return laufDistanzInMeter
    
    }

    这是我的回电

          //Result is always 0.0km
                        val distance = mStartLocation.distanceTo(mEndLocation)
                        tvDistanzKm.text = distance.toString()
    
                        //Testing...No Difference
                      //  mStartLocation == mEndLocation ???????
                        val currentLocation = mStartLocation.latitude
                        tvStart.text = currentLocation.toString()
                        val endLocation = mEndLocation.latitude
                        tvEnd.text = endLocation.toString()
    
                        //Testing the latitude/longitude in TextView
                        tvLatitude.text = location.latitude.toString()
                        tvLongitude.text = location.longitude.toString()
    
                        if (location != null) {
                              if (location.hasSpeed()) {
                                val getkmh = (location.speed * 3.6)
                                tvTempo.text = getkmh.toString()
                                tvSpeed.text = location.speed.toString()
                            } else {
                                tvTempo.text = "0.00"
                                tvSpeed.text = "0.00"
                            }
                            if (location.hasAccuracy()) {
                                tvAccuracy.text = location.accuracy.toString()
                            } else {
                                tvAccuracy.text = "schlechte Ortung"
                            }
                            if (location.hasAltitude()) {
                                tvAltitude.text = location.altitude.toString()
                            } else {
                                tvAltitude.text = "Nix Höhe"
                                Toast.makeText(
                                    applicationContext,
                                    "keine Höhe erkannt",
                                    Toast.LENGTH_SHORT
                                )
                                    .show()
                            }
                             startLocationUpdates()
                            updatePolyline()
                            buttons()
                            onLocationchanged(location)
                            getLaufDistanz()
                        }
    
    我不知道问题出在哪里,所以,请你帮帮我好吗。 多谢各位

    我试过不同的方法,但总是没有结果

    我在网上找不到解决方案,在stackoverflow上也找不到

    我是科特林的新手


    谢谢

    您正在将
    mStartLocation
    mEndLocation
    设置为同一对象,因此它们之间的距离应为0.0。在这里查看您的逻辑:

        if(mStartLocation == null)
        {
            mStartLocation  = mCurrentLocation
            mEndLocation = mCurrentLocation
        }
        else {
            mEndLocation = mCurrentLocation
        }
        //after above lines, both mEndLocation and mStartLocation point at the same object
        mCurrentLocation = location; // this will have no effect on next line
        val distance = mStartLocation.distanceTo(mEndLocation) //doesn't use "location"
    
    在上面的代码中,传递到此函数的最新位置甚至没有用作公式的一部分。我认为应该是这么简单:

    var previousLocation: Location? = null
    var totalDistance = 0f
    
    private fun onLocationchanged(location:Location): Float {
        val distance = previousLocation?.distanceTo(location) ?: 0f
        previousLocation = location
        totalDistance += distance
        tvDistanzKm.text = totalDistance.toString()
        return totalDistance
    }
    
    位置到公里或米-

    private fun distanceLoc(location1: Location, location2: Location): String {
            val theta = location1.longitude - location2.longitude
            var dist = (sin(degToRad(location1.latitude))
                    * sin(degToRad(location2.latitude))
                    * cos(degToRad(location1.latitude))
                    * cos(degToRad(location2.latitude))
                    * cos(degToRad(theta)))
            dist = acos(dist)
            dist = radToDeg(dist)
            dist *= 60.0 * 1.1515
            var distStr = "$dist Km."
            if (dist < 0) {
                dist *= 1000
                distStr = "$dist Meter"
            }
            return distStr
        }
    
        private fun radToDeg(rad: Double): Double {
            return rad * 180 / Math.PI
        }
    
        private fun degToRad(deg: Double): Double {
            return deg * Math.PI / 180.0
        }
    
    private fun distance loc(位置1:Location,位置2:Location):字符串{
    valθ=位置1.经度-位置2.经度
    var dist=(sin(degToRad(位置1.纬度))
    *sin(degToRad(位置2.纬度))
    *cos(degToRad(位置1.纬度))
    *cos(degToRad(位置2.纬度))
    *cos(degToRad(θ)))
    dist=acos(dist)
    dist=radToDeg(dist)
    距离*=60.0*1.1515
    var distStr=“$dist Km。”
    如果(距离<0){
    距离*=1000
    distStr=“$dist-Meter”
    }
    返回区
    }
    私人娱乐radToDeg(rad:Double):Double{
    返回rad*180/Math.PI
    }
    私人娱乐消除器(度:双):双{
    返回度*Math.PI/180.0
    }
    
    Wow.谢谢。它看起来有点复杂,像哈弗森公式,但我会试试。好的,谢谢。如何从距离收集数据以恢复到一个距离?用数组?名单?有这样的例子吗?我的意思是,例如…3米,2米,3米=8米。。。。长度应在一种溶液中恢复。谢谢您是否希望计算出所有距离的总和,或从传递给此函数的第一个位置算起的距离?如果您只需要一个总数,那么只需要在这个函数中添加距离的var:Float。如果您想要与第一个点的距离,请保留对第一个位置的引用,并将其用于距离计算,而不是上一个点。对不起,我不明白。-我想要所有距离的总和。我试图在这个函数中添加一个var,但得到的只是一个崩溃。/它不起作用假设你的起点是伦敦,第二点是北京,第三点是格拉斯哥。所有距离的总和为8143km+7994km=16137km,但从第一个点到最近一个点的距离仅为556km,因为忽略了中间点。你希望它以哪种方式工作?如果你需要一个变量,使它的值保持在函数调用之间,你必须在任何函数之外声明它,这样它将成为类的一个属性。