Skip to content

Instantly share code, notes, and snippets.

@Arunshaik2001
Created October 8, 2022 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arunshaik2001/1916bbb9a884e6ef958896e7234b3e56 to your computer and use it in GitHub Desktop.
Save Arunshaik2001/1916bbb9a884e6ef958896e7234b3e56 to your computer and use it in GitHub Desktop.
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
fun createLocationRequest(context: Context,fusedLocationClient: FusedLocationProviderClient) {
val locationRequest = LocationRequest.create().apply {
interval = 1000
fastestInterval = 1000
priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY
}
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
fusedLocationClient.requestLocationUpdates(locationRequest,object : LocationCallback() {
override fun onLocationResult(p0: LocationResult) {
for (location in p0.locations){
CoroutineScope(Dispatchers.IO).launch {
val weatherDTO = WeatherApi
.apiInstance
.getWeatherDetails(location.latitude,
location.longitude,
context.resources.getString(com.example.wear2.R.string.open_weather_api_key))
dataLoaded.value = true;
data.value = AppCardData(name = weatherDTO.name,
time = "${(weatherDTO.main.temp - 273).roundToInt()}°C",
weatherDescription = weatherDTO.weather[0].description,
temp = (weatherDTO.main.temp - 273).roundToInt().toDouble()
)
}
}
}
}, Looper.getMainLooper())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment