Created
November 27, 2020 15:31
-
-
Save ayetolusamuel/ba0588f3a874e7aafac5bc221dde105d 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
import android.Manifest | |
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.location.Location | |
import android.location.LocationListener | |
import android.location.LocationManager | |
import android.provider.Settings | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.app.ActivityCompat | |
class CurrentLocation(val activity:AppCompatActivity) { | |
private var locationGps: Location? = null | |
private var locationNetwork: Location? = null | |
init { | |
activity.getLocation() | |
} | |
private fun AppCompatActivity.getLocation() { | |
// val uid = Firebase.auth.currentUser?.uid | |
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
val hasGps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) | |
val hasNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) | |
if (hasGps || hasNetwork) { | |
if (hasGps) { | |
if (ActivityCompat.checkSelfPermission(this, | |
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( | |
this, | |
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED | |
) { | |
// TODO: Consider calling | |
// ActivityCompat#requestPermissions | |
// here to request the missing permissions, and then overriding | |
// public void onRequestPermissionsResult(int requestCode, String[] permissions, | |
// int[] grantResults) | |
// to handle the case where the user grants the permission. See the documentation | |
// for ActivityCompat#requestPermissions for more details. | |
return | |
} | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, | |
5000, | |
0F, | |
object : | |
LocationListener { | |
override fun onLocationChanged(p0: Location) { | |
if (p0 != null) { | |
locationGps = p0 | |
val result = | |
"Location GPS:::::::::::Latitude : ${locationGps!!.latitude} Longitude: ${locationGps!!.longitude}" | |
println(result) | |
} | |
} | |
}) | |
val localGpsLocation = | |
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) | |
if (localGpsLocation != null) | |
locationGps = localGpsLocation | |
} | |
if (hasNetwork) { | |
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, | |
5000, | |
0F, | |
object : LocationListener { | |
override fun onLocationChanged(p0: Location) { | |
if (p0 != null) { | |
locationNetwork = p0 | |
val result = | |
"Location Network :::::::: Latitude : ${locationNetwork!!.latitude} Longitude: ${locationNetwork!!.longitude}" | |
println(result) | |
} | |
} | |
}) | |
val localNetworkLocation = | |
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) | |
if (localNetworkLocation != null) | |
locationNetwork = localNetworkLocation | |
} | |
if (locationGps != null && locationNetwork != null) { | |
if (locationGps!!.accuracy > locationNetwork!!.accuracy) { | |
val result = "Location GPS > Network accuracy:::::: Latitude: ${locationGps!!.longitude}, Latitude, ${locationGps!!.latitude}" | |
println(result) | |
} else { | |
val result = | |
"Location Network accuracy is greater than GPS::::::::::::: Latitude: ${locationNetwork!!.longitude},Latitude, ${locationNetwork!!.latitude}" | |
println(result) | |
} | |
} | |
} else { | |
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) | |
} | |
} | |
fun getLocation():LocationData?{ | |
if (locationGps != null || locationNetwork != null){ | |
return LocationData(locationGps!!,locationNetwork!!) | |
} | |
return null | |
} | |
class LocationData(val gpsLocation: Location, val networkLocation: Location) | |
} |
val currentLocation = CurrentLocation(this).getLocation()
if (currentLocation != null){
val locationGps = currentLocation.gpsLocation
println("Location GPS $locationGps")
Toast.makeText(this, "Gps Network $locationGps",Toast.LENGTH_SHORT).show()
val locationNetwork = currentLocation.networkLocation
println("Location Network $locationNetwork")
Toast.makeText(this, "Network Gps $locationNetwork", Toast.LENGTH_SHORT).show()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
class CurrentLocation(val activity:AppCompatActivity) {
class LocationData(val gpsLocation: Location, val networkLocation: Location)
}
`