如何在android GPS中获得5米精度

问题描述

在我的 android 应用程序中,我正在读取用户的位置并将其指定为他们的家庭位置,但是对于某些用户,当他/她再次检查时,显示用户离家超过 200 米,在在这种情况下,用户再次必须重新分配他的位置,我了解到这可能是因为设备的位置准确性。我只是想确保遵循接收 GPS 纬度、经度的做法是好的,或者有什么我可以改进的地方,

以下是清单权限,

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

以下是在变量中存储位置的代码

val fusedLocationClient = FusedLocationProviderClient(application)
private lateinit var locationCallback: LocationCallback
var curLoc = mutablelivedata<Array<Double>>()


val locationRequest = LocationRequest.create()?.apply {
            interval = 5000
            fastestInterval = 2500
            priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        }
        val builder = LocationSettingsRequest.Builder()
            .addLocationRequest(locationRequest!!)

        val client: SettingsClient = LocationServices.getSettingsClient(requireActivity())

        val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
        task.addOnSuccessListener { locationSettingsResponse ->
            // All location settings are satisfied. The client can initialize
            // location requests here.
            // ...

//            val locationCallback: LocationCallback
            locationCallback = object : LocationCallback() {
                override fun onLocationResult(locationResult: LocationResult?) {
                    locationResult ?: return

                    for (location in locationResult.locations) {
                        if (location.isFromMockProvider) {
                            Toast.makeText(
                                requireActivity(),"Mock Location Enabled",Toast.LENGTH_SHORT
                            ).show()
//                                fusedLocationClient.removeLocationUpdates(locationCallback)
                        } else {
                            val lat = location.latitude
                            val lon = location.longitude
                            val arr = arrayOf(lat,lon)
                            curLoc.value = arr
                        }
                    }
                }
            }

            fusedLocationClient.requestLocationUpdates(
                locationRequest,locationCallback,Looper.getMainLooper()
            )
        }
        val REQUEST_CHECK_SETTINGS = 111
        task.addOnFailureListener { exception ->

            if (exception 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().
                    exception.startResolutionForResult(
                        requireActivity(),REQUEST_CHECK_SETTINGS
                    )
                } catch (sendEx: IntentSender.SendIntentException) {
                    // Ignore the error.
                }
            }
        } 

使用上面的代码,我将最后一个位置保存在变量 curloc 中。当用户尝试将位置指定为他们的家时,我正在等待 10 秒(只是给时间以获得更准确的位置)然后将 curLoc 作为他们的家纬度和经度,因此在第 10 秒收到的位置更新将是他们的家经纬度。

这种做法是否可行,或者是否有任何可以在 5 米范围内获得准确位置的良好做法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)