An android library used to track your current location, it provides the latitude, longitude and location for your personal use.
//Add the gif and screen shot below if you have them in the repo
- Include jitpack in your root
settings.gradle
file.
pluginManagement {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- And add it's dependency to your app level
build.gradle
file:
dependencies {
implementation 'com.github.RhymezxCode:TrackMe:1.0.1'
//Dexter for runtime permissions
implementation 'com.karumi:dexter:6.2.3'
}
- First initialize the builder class:
val trackMe = TrackMe.Builder()
.context(context = this)
.build()
- make sure you've accepted permissions for ACCESS_FINE_LOCATION, before checking your location status and using TrackMe to get your location.
//Using Dexter for runtime permissions
private fun permissionCheck() {
Dexter.withContext(this@MainActivity)
.withPermissions(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
.withListener(object : MultiplePermissionsListener {
override fun onPermissionsChecked(p0: MultiplePermissionsReport?) {
if(p0?.areAllPermissionsGranted() == true){
lifecycleScope.launch {
getCurrentLocation()
}
}
}
override fun onPermissionRationaleShouldBeShown(
p0: MutableList<PermissionRequest>?,
p1: PermissionToken?
) {
p1?.continuePermissionRequest()
}
}).check()
}
private suspend fun getCurrentLocation() {
when (trackMe?.checkMyLocationStatus()) {
true -> {
try {
val addresses: List<Address>? = withContext(Dispatchers.IO){
geoCoder.getFromLocation(
trackMe?.getMyLatitude() ?: 0.0,
trackMe?.getMyLongitude() ?: 0.0,
1
)
}
if (!addresses.isNullOrEmpty()) {
Log.e("Your Full Address: ", "$addresses")
val address: String = addresses[0].getAddressLine(0)
val currentLatitude = trackMe?.getMyLatitude() ?: 0.0
val currentLongitude = trackMe?.getMyLongitude() ?: 0.0
val currentAddress = address
val currentLatLng = LatLng(
currentLatitude,
currentLongitude
)
val marker: MarkerOptions = MarkerOptions()
.position(currentLatLng)
.title(currentAddress)
lifecycleScope.launchWhenStarted {
//Using your location to pin a marker on the map
map.addMarker(marker)
map.animateCamera(
CameraUpdateFactory.newLatLngZoom(
currentLatLng,
17f
)
)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
false -> {
lifecycleScope.launchWhenStarted {
showToast(
this@MainActivity,
"Your location is not available yet!"
)
}
}
else -> {}
}
}
- Available methods for your location:
//DataTypes
trackMe?.getMyLatitude() //Double
trackMe?.getMyLongitude() //Double
trackMe?.getMyLocation() //Location
Note: You can run the sample project in the repo, to see how it works!
📌 Please, feel free to give me a star 🌟, I also love sparkles ✨
Developed with 💖 by
Awodire Babajide Samuel