Kotlin-位置跟踪问题

时间:2020-01-02 04:51:43

标签: android kotlin location android-permissions

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

到目前为止,我已经完成了以下操作:

必要变量:

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

在按钮上单击:检查位置的权限,是否具有名为 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,那么我该如何获取位置?

谢谢。

1 个答案:

答案 0 :(得分:1)

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

您将收到类似于onActivityResult的回拨到onActivityReenter的电话。因此,重写该方法并在那里调用getAndSaveCurrentLocation()。