Often we need our user location or need to update user location in our app. This is a simple util class that I build for my pet project. Use this below code as a util class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import android.Manifest import android.annotation.SuppressLint import android.app.Activity import android.content.Context import android.content.IntentSender import android.content.pm.PackageManager import android.os.Looper import android.support.v4.app.ActivityCompat import com.google.android.gms.common.api.ResolvableApiException import com.google.android.gms.location.* import com.google.android.gms.maps.model.LatLng import com.google.android.gms.tasks.Task import com.shaon2017.salahz.interfaces.SimpleCallback /** * Created by Shaon on 4/18/2018. * Bismillahir Rohmanir Rohim :) */ class InitGeoLocationUpdate { companion object { private var locationCallBack: LocationCallback? = null private var fusedLocationClient: FusedLocationProviderClient? = null private val TAG = InitGeoLocationUpdate::class.simpleName private lateinit var locationLatLngCallback : SimpleCallback<LatLng> fun locationInit(context: Context, locationLatLngCallback: SimpleCallback<LatLng>) { this.locationLatLngCallback = locationLatLngCallback createLocationRequest(context, getLocationCallBack()); } private fun getLocationCallBack(): LocationCallback { if (locationCallBack == null) return initLocationCallback() return locationCallBack!! } private fun initLocationCallback(): LocationCallback { locationCallBack = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult?) { super.onLocationResult(locationResult) val mCurrentLocation = locationResult!!.lastLocation val mCurrentLatLng = LatLng(mCurrentLocation!!.latitude, mCurrentLocation.longitude) locationLatLngCallback.callback(mCurrentLatLng) Lg.d(TAG!!, "Current Location Latitude: " + mCurrentLatLng.latitude + " Longitude: " + mCurrentLatLng.longitude) } } return locationCallBack!! } @SuppressLint("RestrictedApi") private fun createLocationRequest(context: Context, locationCallback: LocationCallback) { val locationRequest = LocationRequest().apply { interval = 1000 fastestInterval = 2000 priority = LocationRequest.PRIORITY_HIGH_ACCURACY } buildLocationSettingsRequest(context, locationRequest, locationCallback); } private fun getFusedLocationClient(context: Context): FusedLocationProviderClient? { if (fusedLocationClient == null) return createFusedLocationClient(context) return fusedLocationClient } private fun createFusedLocationClient(context: Context): FusedLocationProviderClient? { fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) return fusedLocationClient } private val REQUEST_CHECK_SETTINGS = 0x1 private fun buildLocationSettingsRequest(context: Context, locationRequest: LocationRequest, locationCallback: LocationCallback) { val builder = LocationSettingsRequest.Builder(); builder.addLocationRequest(locationRequest).setAlwaysShow(true); // val mLocationSettingsRequest = builder.build(); val client: SettingsClient = LocationServices.getSettingsClient(context) val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build()) task.addOnSuccessListener(context as Activity, { if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) getFusedLocationClient(context)!!.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper()); }).addOnFailureListener { if (it is ResolvableApiException) { // Location settings are not satisfied, but this can be fixed // by showing the user a dialog. try { // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). it.startResolutionForResult(context as Activity, REQUEST_CHECK_SETTINGS) } catch (sendEx: IntentSender.SendIntentException) { // Ignore the error. } } } } fun stopLocationUpdate(context: Context) { getFusedLocationClient(context as Activity)!!.removeLocationUpdates(getLocationCallBack()) } } } |
How to use? Follow the below code call this below code when you need location update. This…