简体   繁体   English

Kotlin - 位置跟踪问题

[英]Kotlin - Issue in Location tracking

I must have to take latitude and longitude of the user when user first time open an application.当用户第一次打开应用程序时,我必须获取用户的纬度和经度。

So far, I have done as below :到目前为止,我做了如下:

Necessary Variables :必要的变量:

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

On Button Click : Checking the permission for the location, if has permission calling method named onLocationGranted() else asking for the location permission. 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
                    )
                }

Below is the method onLocationGranted() : Here, Initializing LocationManager, Checking that GPS is on or not.下面是onLocationGranted()方法: 这里,初始化 LocationManager,检查 GPS 是否打开。 If GPS on or enabled, taking location which is done in method named : getAndSaveCurrentLocation() .如果 GPS 打开或启用,则在名为getAndSaveCurrentLocation() 的方法中获取位置。 If, GPS is off or not enabled calling the method named buildAlertMessageNoGps()如果 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()
}

Method getAndSaveCurrentLocation() is as below : In which I am taking location using fusedapi.方法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()
    }
}

Method buildAlertMessageNoGps() is as below : If GPS is off then, I am calling this method.方法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()
    }

Now, the above method opens the settings to turn on GPS.现在,上述方法打开设置以打开 GPS。

My question is : Suppose user turns on GPS, and coming back to screen, How can I get location then ?我的问题是:假设用户打开 GPS,然后返回屏幕,那么我如何获取位置? Or If user not turning on GPS there, in that case How can I take location?或者如果用户没有在那里打开 GPS,在这种情况下我如何定位?

Thanks.谢谢。

Suppose user turns on GPS, and coming back to screen, How can I get location then ?假设用户打开 GPS,然后返回屏幕,那么我如何获取位置?

You will get a call back to onActivityReenter similar to onActivityResult.你会得到一个回调到onActivityReenter类似onActivityResult。 So Override that method and call getAndSaveCurrentLocation() there.所以覆盖该方法并在那里调用 getAndSaveCurrentLocation() 。

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

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