Pro Android with Kotlin


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

176
CHAPTER 8: APIs
The Android OS itself contains a location framework with classes in the package android.
location. However, the official position of Google is to favor the Google Play Services 
Location API because it is more elaborate and simpler to use. We follow this suggestion 
and talk about the services API in the following paragraphs. Location is about finding out 
the geographical position of a device as a latitude-longitude pair and finding out the street 
names, house numbers, and other points of interest given the geographical position.
To make the Services Location API available to your app, add the following as dependencies 
in your app module’s build.gradle file (as just two lines; remove the newlines after 
implementation):
implementation
'com.google.android.gms:play-services-location:11.8.0'
implementation
'com.google.android.gms:play-services-maps:11.8.0'
Last Known Location
The easiest way to get the device’s position is to get the last known location. To do so, in 
your app request permissions, add the following:
"android.permission.ACCESS_COARSE_LOCATION"/>
"android.permission.ACCESS_FINE_LOCATION"/>
GPS resolution with fewer than ten yards needs FINE location permission, while the 
coarser network-based resolution with approximately 100 yards needs the COARSE location 
permission. Adding both to the manifest file gives us the most options, but depending on 
your needs, you could continue with just the coarse one.
Then, inside you component (for example, inside onCreate()), you construct a 
FusedLocationProviderClient as follows:
var fusedLocationClient: FusedLocationProviderClient? =
null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
fusedLocationClient = LocationServices.
getFusedLocationProviderClient(this)
}
Wherever needed in your app, you can use it to get the last known location.
if (checkPermission(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION)) {



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   147   148   149   150   151   152   153   154   ...   223




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