Skip to content

Instantly share code, notes, and snippets.

@manorgass
Created March 28, 2021 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manorgass/9fcae660c973c7cd260fbc5e35386849 to your computer and use it in GitHub Desktop.
Save manorgass/9fcae660c973c7cd260fbc5e35386849 to your computer and use it in GitHub Desktop.
package com.example.fusedlocationtest.provider
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Looper
import android.util.Log
import androidx.core.app.ActivityCompat
import com.example.fusedlocationtest.OnLocationUpdateListener
import com.google.android.gms.location.*
class FusedLocationProvider(
private val context: Context,
private val listener: OnLocationUpdateListener
) {
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private lateinit var locationCallback: LocationCallback
init {
initLocationClient()
initLocationCallback()
}
private fun initLocationClient() {
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
val locationRequest = LocationRequest.create()?.apply {
interval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
val builder = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest!!)
val client = LocationServices.getSettingsClient(context)
val task = client.checkLocationSettings(builder.build())
task.addOnSuccessListener {
Log.d(TAG, "location client setting success")
}
task.addOnFailureListener {
Log.d(TAG, "location client setting failure")
}
}
private fun initLocationCallback() {
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations) {
listener.onLocationUpdated(location)
break
}
}
}
}
fun startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
val locationRequest = LocationRequest.create()?.apply {
interval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
fusedLocationProviderClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
fun requestLastLocation() {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
fusedLocationProviderClient.lastLocation
.addOnSuccessListener { location ->
listener.onLocationUpdated(location)
}
}
fun stopLocationUpdates() {
fusedLocationProviderClient.removeLocationUpdates(locationCallback)
}
companion object {
private const val TAG = "FusedLocationManager"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment