【问题标题】:Kotlin Location is far from my location How can i make it more accurityKotlin Location is far from my location 我怎样才能让它更准确
【发布时间】:2020-02-21 12:28:17
【问题描述】:
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 远。我怎样才能使它更准确? 我为我的设备获取经度和纬度,但是当我在谷歌地图中显示时,它给出了 200 米外的位置,我检查了经度纬度和谷歌地图。 而且经纬度的失败离我很远。

【问题讨论】:

    标签: android kotlin location


    【解决方案1】:

    请试试这个!

    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
    }
    

    【讨论】:

    • iss AppConstant 一个库,因为我的 android studio 无法定义它
    • @OğuzKurukaya AppConstant 只是我在全球范围内使用的常量类。
    • 所以我不能使用没有它的代码?我该如何创建它
    • @OğuzKurukaya 我已经更新了您可以检查的代码,它没有必要使用。我已经在整个应用程序中使用过,所以我在全球范围内花了很长时间。
    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2021-05-27
    • 2012-03-23
    • 1970-01-01
    • 2017-09-07
    • 2011-06-14
    • 2014-07-05
    相关资源
    最近更新 更多