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
class LocationLiveData(context: Context) : LiveData<LocationModel>() { | |
@SuppressLint("MissingPermission") | |
private fun startLocationUpdates() { | |
fusedLocationClient.requestLocationUpdates( | |
locationRequest, | |
locationCallback, | |
null | |
) | |
} |
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
class LocationLiveData(context: Context) : LiveData<LocationModel>() { | |
private val locationCallback = object : LocationCallback() { | |
override fun onLocationResult(locationResult: LocationResult?) { | |
locationResult ?: return | |
for (location in locationResult.locations) { | |
setLocationData(location) | |
} | |
} | |
} |
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
class LocationLiveData(context: Context) : LiveData<LocationModel>() { | |
companion object { | |
val locationRequest: LocationRequest = LocationRequest.create().apply { | |
interval = 10000 | |
fastestInterval = 5000 | |
priority = LocationRequest.PRIORITY_HIGH_ACCURACY | |
} | |
} | |
} |
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
class LocationLiveData(context: Context) : LiveData<LocationModel>() { | |
private fun setLocationData(location: Location) { | |
value = LocationModel( | |
longitude = location.longitude, | |
latitude = location.latitude | |
) | |
} | |
} |
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
data class LocationModel( | |
val longitude: Double, | |
val latitude: Double | |
) |
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
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> |
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
class LocationLiveData(context: Context) : LiveData<LocationModel>() { | |
private var fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) | |
override fun onInactive() { | |
super.onInactive() | |
} | |
override fun onActive() { | |
super.onActive() |
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
class LocationActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_location) | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".LocationActivity"> | |
<TextView |
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
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | |
implementation 'androidx.appcompat:appcompat:1.0.2' | |
implementation 'androidx.core:core-ktx:1.0.2' | |
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | |
implementation 'com.google.android.gms:play-services-location:17.0.0' | |
implementation "android.arch.lifecycle:extensions:1.1.1" |