繁体   English   中英

Kotlin - 位置跟踪问题

[英]Kotlin - Issue in Location tracking

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

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

必要的变量:

 //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,在这种情况下我如何定位?

谢谢。

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

你会得到一个回调到onActivityReenter类似onActivityResult。 所以覆盖该方法并在那里调用 getAndSaveCurrentLocation() 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM