Skip to content

Instantly share code, notes, and snippets.

@RhymezxCode
Last active October 6, 2023 12:58
Show Gist options
  • Save RhymezxCode/144ca6cfb8ada33692155f56610c0398 to your computer and use it in GitHub Desktop.
Save RhymezxCode/144ca6cfb8ada33692155f56610c0398 to your computer and use it in GitHub Desktop.
TrackMe readme file

TrackMe Android Library

TrackMe Android Library least API level TrackMe Android Library on jitpack.io TrackMe Android Library License. TrackMe Android Library Stars TrackMe Android Library Forks TrackMe Android Library Issues TrackMe Android Library Issues

TrackMe Android Library

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

1. Adding TrackMe to your project

  • 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'
}

Sync your project, and 😱 boom 🔥 you have added TrackMe successfully. ❗

2. Usage

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment