Skip to content

Commit

Permalink
Mapbox Autocomplete: catch HttpExceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Jul 31, 2024
1 parent f59fd9b commit 6ab3ba2
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.mapbox.api.geocoding.v5.models.CarmenFeature
import com.mapbox.geojson.BoundingBox
import com.mapbox.geojson.Point
import net.vonforst.evmap.R
import retrofit2.HttpException
import java.io.IOException

class MapboxAutocompleteProvider(val context: Context) : AutocompleteProvider {
Expand All @@ -25,15 +26,20 @@ class MapboxAutocompleteProvider(val context: Context) : AutocompleteProvider {
override val id = "mapbox"

override fun autocomplete(query: String, location: LatLng?): List<AutocompletePlace> {
val result = MapboxGeocoding.builder().apply {
val request = MapboxGeocoding.builder().apply {
location?.let {
proximity(Point.fromLngLat(location.longitude, location.latitude))
}
languages(ConfigurationCompat.getLocales(context.resources.configuration)[0]?.language)
accessToken(context.getString(R.string.mapbox_key))
autocomplete(true)
this.query(query)
}.build().executeCall()
}
val result = try {
request.build().executeCall()
} catch (e: HttpException) {
throw IOException(e)
}
if (!result.isSuccessful) {
throw IOException(result.message())
}
Expand Down

0 comments on commit 6ab3ba2

Please sign in to comment.