Created
November 1, 2019 14:06
-
-
Save yudikarma/0e787e0733c867d7d09d7fcf6070c153 to your computer and use it in GitHub Desktop.
get current location
This file contains hidden or 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
implementation 'com.google.android.gms:play-services-location:17.0.0' |
This file contains hidden or 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
//call this method in your activity | |
private lateinit var androidViewModels: NearbyLocationAndroidViewModels | |
androidViewModels = ViewModelProviders.of(this).get(NearbyLocationAndroidViewModels::class.java) | |
fun startLocationUpdate(savedInstanceState: Bundle?){ | |
androidViewModels.getLocationData().observe(this, Observer { | |
Log.d("locatin is"," ${it.lat} , ${it.lng}") | |
}) | |
} |
This file contains hidden or 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.annotation.SuppressLint | |
import android.content.Context | |
import androidx.lifecycle.LiveData | |
import com.google.android.gms.location.LocationCallback | |
import com.google.android.gms.location.LocationRequest | |
import com.google.android.gms.location.LocationResult | |
import com.google.android.gms.location.LocationServices | |
import es.hyrax.zonapets.data.network.model.nearby_locations.Location | |
/** | |
* Class FOR GET CURRENT LOCATRION USER DEVICE | |
* */ | |
class LocationLiveData (context: Context) : LiveData<Location>() { | |
companion object { | |
val locationRequest: LocationRequest = LocationRequest.create().apply { | |
interval = 10000000 | |
fastestInterval = 5000 | |
priority = LocationRequest.PRIORITY_HIGH_ACCURACY | |
} | |
} | |
private var fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) | |
override fun onInactive() { | |
super.onInactive() | |
fusedLocationClient.removeLocationUpdates(locationCallback) | |
} | |
override fun onActive() { | |
super.onActive() | |
fusedLocationClient.lastLocation | |
.addOnSuccessListener { | |
location: android.location.Location? -> | |
location?.also { | |
setLocationData(it) | |
} | |
} | |
startLocationUpdates() | |
} | |
private fun setLocationData(location: android.location.Location) { | |
value = Location( | |
lat = location.latitude, | |
lng = location.longitude | |
) | |
} | |
private val locationCallback = object : LocationCallback() { | |
override fun onLocationResult(locationResult: LocationResult?) { | |
locationResult ?: return | |
for (location in locationResult.locations) { | |
setLocationData(location) | |
} | |
} | |
} | |
@SuppressLint("MissingPermission") | |
private fun startLocationUpdates() { | |
fusedLocationClient.requestLocationUpdates( | |
locationRequest, | |
locationCallback, | |
null | |
) | |
} | |
} |
This file contains hidden or 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.app.Application | |
import androidx.lifecycle.AndroidViewModel | |
import es.hyrax.zonapets.utils.LocationLiveData | |
/** Android viewmodel for Nearby Locatin Activity | |
*/ | |
class NearbyLocationAndroidViewModels(application: Application):AndroidViewModel(application) { | |
/** get location from Location LIve Data | |
*/ | |
private val locationData = LocationLiveData(application) | |
/** action for GetLocation | |
*/ | |
fun getLocationData() = locationData | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment