Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet154/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   150   151   152   153   154   155   156   157   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

180
CHAPTER 8: APIs
this@MainActivity,
REQUEST_CHECK_STATE)
}
}
This asynchronously performs a check. If high accuracy is requested and the device’s 
setting won’t allow updates based on GPS data, the corresponding system settings dialog 
gets called. The latter happens somewhat awkwardly inside the exception catch. The result 
of the corresponding system intent call ends up in the following:
override fun onActivityResult(requestCode: Int,
resultCode: Int, data: Intent) {
if (requestCode and 0xFFFF == REQUEST_CHECK_STATE) {
Log.e("LOG", "Back from REQUEST_CHECK_STATE")
...
}
}
With all set up correctly and enough permissions, we now can register for location updates 
via the following:
val locationUpdates = object : LocationCallback() {
override fun onLocationResult(lr: LocationResult) {
Log.e("LOG", lr.toString())
Log.e("LOG", "Newest Location: " + lr.locations.
last())
// do something with the new location...
}
}
fusedLocationClient?.requestLocationUpdates(reqSetting,
locationUpdates,
null /* Looper */)
To stop location updates, you move locationUpdates to a class field and react to stop 
requests via the following:
fun stopPeriodic(view:View) {
fusedLocationClient?.
removeLocationUpdates(locationUpdates)
}
Geocoding
The Geocoder class allows you to determine the geocoordinates (longitude, latitude) for a 
given address or, conversely, possible addresses for given geocoordinates. These processes 
are known as forward and reverse geocoding. The Geocoder class internally uses an online 
Google service, but the details are hidden inside the implementation. You as a developer can 
use the Geocoder class without the need to understand where the data come from.
This section is about reverse geocoding. We use a Location object with longitude and 
latitude to find nearby street names. To start, we first have to decide what we do with a 
potentially long-running operation. To do the lookup, a network operation is necessary, and 



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   150   151   152   153   154   155   156   157   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling