Skip to content

Instantly share code, notes, and snippets.

@NaCI
Last active November 13, 2020 13:27
Show Gist options
  • Save NaCI/ad33034dad0e39f2e57757659b745864 to your computer and use it in GitHub Desktop.
Save NaCI/ad33034dad0e39f2e57757659b745864 to your computer and use it in GitHub Desktop.
import android.location.Location
import com.example.hmsgmssinglecodebase.LocationHelper;
class DashboardActivity : AppCompatActivity(), LocationHelper.GlobalLocationCallback {
...
companion object {
...
const val REQUEST_CHECK_SETTINGS = 999
}
private lateinit var locationHelper: LocationHelper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
locationHelper = LocationHelper(activity = this, fragment = null, globalLocationCallback = this, requestCode = REQUEST_CHECK_SETTINGS)
}
// Result handled after user enables gps
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
...
if (requestCode == REQUEST_CHECK_SETTINGS) {
if (resultCode == RESULT_OK) {
requestLocationUpdate()
}
}
}
// Start location request after permission granted
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
fun getCurrentLocation() {
locationHelper.requestLocationUpdatesWithSettingsCheck()
}
// GlobalLocationCallback methods
override fun onLocationResult(location: Location) {
currentLocation = location
// Do whatever u want with location information
progressDialog.setVisibility(View.GONE)
}
override fun onLocationFailed() {
progressDialog.setVisibility(View.GONE)
// Do whatever u want on error, show snackbar maybe
}
override fun onLocationRequest() {
// Start progressDialog till location data fetch operation ends
progressDialog.setVisibility(View.VISIBLE)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment