in LocationAddressKotlin/app/src/main/java/com/google/android/gms/location/sample/locationaddress/FetchAddressIntentService.kt [56:140]
override fun onHandleIntent(intent: Intent?) {
var errorMessage = ""
receiver = intent?.getParcelableExtra(Constants.RECEIVER)
// Check if receiver was properly registered.
if (intent == null || receiver == null) {
Log.wtf(TAG, "No receiver received. There is nowhere to send the results.")
return
}
// Get the location passed to this service through an extra.
val location = intent.getParcelableExtra<Location>(Constants.LOCATION_DATA_EXTRA)
// Make sure that the location data was really sent over through an extra. If it wasn't,
// send an error error message and return.
if (location == null) {
errorMessage = getString(R.string.no_location_data_provided)
Log.wtf(TAG, errorMessage)
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage)
return
}
// Errors could still arise from using the Geocoder (for example, if there is no
// connectivity, or if the Geocoder is given illegal location data). Or, the Geocoder may
// simply not have an address for a location. In all these cases, we communicate with the
// receiver using a resultCode indicating failure. If an address is found, we use a
// resultCode indicating success.
// The Geocoder used in this sample. The Geocoder's responses are localized for the given
// Locale, which represents a specific geographical or linguistic region. Locales are used
// to alter the presentation of information such as numbers or dates to suit the conventions
// in the region they describe.
val geocoder = Geocoder(this, Locale.getDefault())
// Address found using the Geocoder.
var addresses: List<Address> = emptyList()
try {
// Using getFromLocation() returns an array of Addresses for the area immediately
// surrounding the given latitude and longitude. The results are a best guess and are
// not guaranteed to be accurate.
addresses = geocoder.getFromLocation(
location.latitude,
location.longitude,
// In this sample, we get just a single address.
1)
} catch (ioException: IOException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available)
Log.e(TAG, errorMessage, ioException)
} catch (illegalArgumentException: IllegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used)
Log.e(TAG, "$errorMessage. Latitude = $location.latitude , " +
"Longitude = $location.longitude", illegalArgumentException)
}
// Handle case where no address was found.
if (addresses.isEmpty()) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found)
Log.e(TAG, errorMessage)
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage)
} else {
val address = addresses[0]
// Fetch the address lines using {@code getAddressLine},
// join them, and send them to the thread. The {@link android.location.address}
// class provides other options for fetching address details that you may prefer
// to use. Here are some examples:
// getLocality() ("Mountain View", for example)
// getAdminArea() ("CA", for example)
// getPostalCode() ("94043", for example)
// getCountryCode() ("US", for example)
// getCountryName() ("United States", for example)
val addressFragments = with(address) {
(0..maxAddressLineIndex).map { getAddressLine(it) }
}
Log.i(TAG, getString(R.string.address_found))
deliverResultToReceiver(Constants.SUCCESS_RESULT,
addressFragments.joinToString(separator = "\n"))
}
}