parent
e470e37101
commit
9b9c71474f
@ -0,0 +1,5 @@
|
||||
package uca.baptistearthur.geocaching.model
|
||||
|
||||
data class Address(
|
||||
val country: String,
|
||||
val displayName: String)
|
@ -1,6 +1,18 @@
|
||||
package uca.baptistearthur.geocaching.model
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.*
|
||||
import org.osmdroid.util.GeoPoint
|
||||
import uca.baptistearthur.geocaching.network.AddressNetwork
|
||||
|
||||
data class Place(
|
||||
val latitude: Double,
|
||||
val longitude: Double
|
||||
)
|
||||
class Place(private val lat: Double,
|
||||
private val lon: Double,
|
||||
var address: Address = Address("unknown", "unknown"))
|
||||
: GeoPoint(lat, lon){
|
||||
suspend fun initAddress() {
|
||||
AddressNetwork.retrofit.let {
|
||||
CoroutineScope(Dispatchers.IO).async {
|
||||
address = it.getAddress(lat, lon)
|
||||
}.await()
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package uca.baptistearthur.geocaching.network
|
||||
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.Headers
|
||||
import retrofit2.http.Query
|
||||
import uca.baptistearthur.geocaching.model.Address
|
||||
|
||||
interface AddressAPI {
|
||||
@GET("/v1/reverse")
|
||||
suspend fun getAddress(
|
||||
@Query("lat") lat: Double,
|
||||
@Query("lon") lon: Double
|
||||
): Address
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package uca.baptistearthur.geocaching.network
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.JsonDeserializationContext
|
||||
import com.google.gson.JsonDeserializer
|
||||
import com.google.gson.JsonElement
|
||||
import uca.baptistearthur.geocaching.model.Address
|
||||
import java.lang.reflect.Type
|
||||
|
||||
class AddressDeserializer : JsonDeserializer<Address> {
|
||||
|
||||
override fun deserialize(
|
||||
json: JsonElement?,
|
||||
typeOfT: Type?,
|
||||
context: JsonDeserializationContext?
|
||||
): Address = json?.asJsonObject!!.let{
|
||||
Address(
|
||||
it.get("address").asJsonObject.get("country").asString,
|
||||
it.get("display_name").asString
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package uca.baptistearthur.geocaching.network
|
||||
|
||||
import com.google.gson.GsonBuilder
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Converter
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import uca.baptistearthur.geocaching.model.Address
|
||||
import java.lang.reflect.Type
|
||||
|
||||
|
||||
object AddressNetwork {
|
||||
private val base_url = "https://forward-reverse-geocoding.p.rapidapi.com/v1/reverse/"
|
||||
private val key = "19516a9900mshce10de76f99976bp10f192jsn8c8d82222baa"
|
||||
|
||||
private val interceptor = HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
}
|
||||
|
||||
private val gson = GsonBuilder().apply {
|
||||
registerTypeAdapter(Address::class.java, AddressDeserializer())
|
||||
}.create()
|
||||
|
||||
private val client = OkHttpClient.Builder()
|
||||
.addInterceptor { chain ->
|
||||
val request = chain.request().newBuilder()
|
||||
.addHeader("X-RapidAPI-Key", key)
|
||||
.build()
|
||||
chain.proceed(request)
|
||||
}
|
||||
.addInterceptor(interceptor)
|
||||
.build()
|
||||
|
||||
val retrofit: AddressAPI by lazy {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(base_url)
|
||||
.client(client)
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.build()
|
||||
.create(AddressAPI::class.java)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue