簡體   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