Created
March 28, 2021 11:41
-
-
Save manorgass/9fcae660c973c7cd260fbc5e35386849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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