【问题标题】:Kotlin - Issue in Location trackingKotlin - 位置跟踪问题
【发布时间】:2020-04-20 19:52:55
【问题描述】:

当用户第一次打开应用程序时,我必须获取用户的纬度和经度。

到目前为止,我做了如下:

必要的变量:

 //Location Utils below :
    private lateinit var fusedLocationClient: FusedLocationProviderClient
    private lateinit var lastLocation: Location
    private lateinit var currentLatLng: LatLng

On Button Click : 检查位置的权限,如果有权限调用方法名为 onLocationGranted() 否则要求位置权限。

if (EasyPermissions.hasPermissions(
                        mContext,
                        FilePickerConst.PERMISSIONS_FINE_LOCATION
                    )
                ) {
                    onLocationGranted()
                } else {
                    // Ask for one permission
                    EasyPermissions.requestPermissions(
                        this,
                        getString(R.string.permission_location),
                        Constant.MultiMediaRequestCode.LOCATION_PERMISSION,
                        FilePickerConst.PERMISSIONS_FINE_LOCATION
                    )
                }

下面是方法onLocationGranted():这里,初始化LocationManager,检查GPS是否开启。如果 GPS 开启或启用,则在名为 getAndSaveCurrentLocation() 的方法中获取位置。如果 GPS 关闭或未启用,则调用名为 buildAlertMessageNoGps()

的方法
val manager = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager    
//If GPS is not Enabled..
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
   buildAlertMessageNoGps()
} else {
   getAndSaveCurrentLocation()
}

方法 getAndSaveCurrentLocation() 如下:我在其中使用 fusedapi 进行定位。

private fun getAndSaveCurrentLocation() {
    try {
        fusedLocationClient =
            LocationServices.getFusedLocationProviderClient(mContext as AppBaseActivity)

        fusedLocationClient.lastLocation.addOnSuccessListener(mContext as AppBaseActivity) { location ->
            // Got last known location. In some rare situations this can be null.
            if (location != null) {
                lastLocation = location
                currentLatLng = LatLng(location.latitude, location.longitude)
                if (currentLatLng != null &&
                    currentLatLng.latitude != null &&
                    currentLatLng.longitude != null
                ) {
                    sharedPreferenceManager?.setStringData(
                        Constant.PrefKey.userLatitude,
                        "" + currentLatLng.latitude
                    )

                    sharedPreferenceManager?.setStringData(
                        Constant.PrefKey.userLongitude,
                        "" + currentLatLng.longitude
                    )
                }

            }
            (mContext as AppBaseActivity).supportFragmentManager.popBackStack()
            (activity as AppBaseActivity).addFragmentToRoot(
                DashboardFragmentNew.newInstance(),
                false
            )
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

方法buildAlertMessageNoGps()如下:如果GPS关闭,我调用这个方法。

private fun buildAlertMessageNoGps() {
        val builder = AlertDialog.Builder(mContext)
        builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
            .setCancelable(false)
            .setPositiveButton("Yes", object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface, id: Int) {
                    startActivity(Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS))
                }
            })
            .setNegativeButton("No", object : DialogInterface.OnClickListener {
                override fun onClick(dialog: DialogInterface, id: Int) {
                    dialog.cancel()
                }
            })
        val alert = builder.create()
        alert.show()
    }

现在,上述方法打开设置以打开 GPS。

我的问题是:假设用户打开 GPS,然后返回屏幕,那么我该如何获取位置? 或者如果用户没有在那里打开 GPS,在这种情况下我该如何定位?

谢谢。

【问题讨论】:

  • 对于您的第一个问题,您可以通过onResume() 方法获取用户位置,对于您的第二个问题,您可以从网络提供商处获取大致位置。看到这个链接stackoverflow.com/questions/6694391/…

标签: android kotlin location android-permissions


【解决方案1】:

假设用户打开 GPS,然后返回屏幕,那么我该如何获取位置?

您将收到与 onActivityResult 类似的回电至onActivityReenter。所以重写该方法并在那里调用 getAndSaveCurrentLocation()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多