Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing location request creation #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mindorks.example.fusedlocation

import android.annotation.SuppressLint
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
Expand All @@ -12,38 +13,43 @@ import com.google.android.gms.location.LocationServices
import com.mindorks.example.fusedlocation.utils.PermissionUtils
import kotlinx.android.synthetic.main.activity_main.*

const val LOCATION_PERMISSION_REQUEST_CODE = 999

class MainActivity : AppCompatActivity() {

companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 999
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

@SuppressLint("MissingPermission")
private fun setUpLocationListener() {
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// for getting the current location update after every 2 seconds with high accuracy
val locationRequest = LocationRequest().setInterval(2000).setFastestInterval(2000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
val locationRequest = LocationRequest.create().also {
it.interval = 2000
it.fastestInterval = 2000
it.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
latTextView.text = location.latitude.toString()
lngTextView.text = location.longitude.toString()
}
// Few more things we can do here:
// For example: Update the location of user on server
locationRequest,
object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
super.onLocationResult(locationResult)
for (location in locationResult.locations) {
latTextView.text = location.latitude.toString()
lngTextView.text = location.longitude.toString()
}
},
Looper.myLooper()
// Few more things we can do here:
// For example: Update the location of user on server
}
},
Looper.myLooper()
)
}


override fun onStart() {
super.onStart()
when {
Expand All @@ -59,17 +65,17 @@ class MainActivity : AppCompatActivity() {
}
else -> {
PermissionUtils.requestAccessFineLocationPermission(
this,
LOCATION_PERMISSION_REQUEST_CODE
this,
LOCATION_PERMISSION_REQUEST_CODE
)
}
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
Expand All @@ -85,12 +91,13 @@ class MainActivity : AppCompatActivity() {
}
} else {
Toast.makeText(
this,
getString(R.string.location_permission_not_granted),
Toast.LENGTH_LONG
this,
getString(R.string.location_permission_not_granted),
Toast.LENGTH_LONG
).show()
}
}
}
}
}
}