android - Kotlin位置离我的位置很远我该如何使其更加准确

标签 android kotlin location

private lateinit var fusedLocationClient: FusedLocationProviderClient
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val name=findViewById(R.id.editText) as TextView
        var text=""
        locationManager = getSystemService(LOCATION_SERVICE) as LocationManager?

        val button =findViewById(R.id.button) as Button
        var mainlink=""
        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)   == PackageManager.PERMISSION_GRANTED) {
            val adresstext= findViewById(R.id.editText3)as TextView
            var deneme=""
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
            fusedLocationClient.lastLocation.addOnSuccessListener {

            }
            fusedLocationClient.lastLocation.addOnSuccessListener {
                    location -> location

                var locationx=location.longitude
                var locationy=location.latitude

                val name = findViewById(R.id.textView) as TextView
                var geocoder: Geocoder
                var listadress :List<Address>


                geocoder = Geocoder(this, Locale.getDefault());
                listadress =geocoder.getFromLocation(locationy,locationx,1)
                var adress=listadress[0].getAddressLine(0)
                adresstext.text=adress
                text=deneme
                mainlink="http://maps.google.com/maps?q="+locationy+","+locationx+""
                mainlink=mainlink+" "+text
                text=mainlink
            }
        }
        else
        {
            val permission= arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION)
            ActivityCompat.requestPermissions(this, permission,0)
        }

一切都还好,但是当它定位时离我200m远。如何使其更准确?
我将设备的经度和纬度作为参数,但是当我在Google map 中显示时,它给出了200 m的位置,因此我检查了经度和谷歌 map 。
而纬度和经度上的失败离我很远。

最佳答案

请尝试一下!

private fun requestLocationUpdates() {
        var mLocationRequest = LocationRequest()
        mLocationRequest!!.setInterval(AppConstants.LOCATION_INTERVAL.toLong())
        mLocationRequest!!.setFastestInterval(AppConstants.FASTEST_LOCATION_INTERVAL.toLong())
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
        var client: FusedLocationProviderClient =
            LocationServices.getFusedLocationProviderClient(applicationContext)

        val permission = ContextCompat.checkSelfPermission(
            applicationContext,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
        if (permission == PackageManager.PERMISSION_GRANTED) {
            // Request location updates and when an update is
            client.requestLocationUpdates(mLocationRequest, object : LocationCallback() {
                override fun onLocationResult(locationResult: LocationResult?) {
                    val location = locationResult!!.lastLocation
                    if (location != null) {
                        latitude = location!!.latitude
                        longitude = location!!.longitude

                        AppConstants.CURRENT_LATITUDE = location!!.latitude
                        AppConstants.CURRENT_LONGITUDE = location!!.longitude

                    }
                }
            }, null)
        }
    }

class AppConstants {
 var CURRENT_LATITUDE = 0.0
 var CURRENT_LONGITUDE = 0.0
}

关于android - Kotlin位置离我的位置很远我该如何使其更加准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338675/

相关文章:

android - 无法获取经纬度

java - 在接口(interface)中创建一个定期调用的方法 - Java

android - 在没有模拟器的情况下测试 Android 浏览器?

java - 非法状态异常 : Unable to add several TextViews in a View

kotlin,注解类型之前的@param是做什么用的

kotlin - 嵌套协方差在 Kotlin 中是如何工作的?

android - 在 Android 应用程序中有一个 "back"按钮是否合理?

android - SherlockActionBar 样式不适用于 ICS

Kotlin 原生相当于 Retrofit

swift - 无法在闭包中快速返回变量