Adding calls to reverse geocoding API using retrofit

master
Arthur VALIN 2 years ago
parent e470e37101
commit 9b9c71474f

@ -57,5 +57,6 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.7.2' implementation 'com.squareup.okhttp3:okhttp:4.7.2'
implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
} }

@ -1,14 +1,16 @@
package uca.baptistearthur.geocaching.converters package uca.baptistearthur.geocaching.converters
import android.os.Build import android.util.Log
import androidx.annotation.RequiresApi
import androidx.room.TypeConverter import androidx.room.TypeConverter
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import org.osmdroid.util.GeoPoint import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import uca.baptistearthur.geocaching.model.Address
import uca.baptistearthur.geocaching.model.Place import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTripEntity import uca.baptistearthur.geocaching.model.RoadTripEntity
import java.time.format.DateTimeFormatter
import java.util.* import java.util.*
class Converters { class Converters {
@ -23,13 +25,13 @@ class Converters {
} }
@TypeConverter @TypeConverter
fun toString(places: List<GeoPoint>): String { fun toString(places: List<Place>): String {
return Gson().toJson(places) return Gson().toJson(places)
} }
@TypeConverter @TypeConverter
fun toPlaces(value: String): List<GeoPoint> { fun toPlaces(value: String): List<Place> {
val listType = object : TypeToken<List<GeoPoint>>() {}.type val listType = object : TypeToken<List<Place>>() {}.type
return Gson().fromJson(value, listType) return Gson().fromJson(value, listType)
} }

@ -1,6 +1,7 @@
package uca.baptistearthur.geocaching.data package uca.baptistearthur.geocaching.data
import org.osmdroid.util.GeoPoint import org.osmdroid.util.GeoPoint
import uca.baptistearthur.geocaching.model.Address
import uca.baptistearthur.geocaching.model.Place import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTripEntity import uca.baptistearthur.geocaching.model.RoadTripEntity
import java.util.Date import java.util.Date
@ -12,25 +13,25 @@ class Stub {
1, 1,
"France", "France",
Date(), Date(),
listOf(GeoPoint(49.3, 49.3)).toMutableList() listOf(Place(49.3, 49.3)).toMutableList()
), ),
RoadTripEntity( RoadTripEntity(
2, 2,
"Italie", "Italie",
Date(), Date(),
listOf(GeoPoint(48.866667, 2.34533), GeoPoint(98.866667, 2.333333)).toMutableList() listOf(Place(48.866667, 2.34533), Place(98.866667, 2.333333)).toMutableList()
), ),
RoadTripEntity( RoadTripEntity(
3, 3,
"Danemark", "Danemark",
Date(), Date(),
listOf(GeoPoint(48.866667, 2.333333), GeoPoint(48.866667, 2.333333)).toMutableList() listOf(Place(48.866667, 2.333333), Place(48.866667, 2.333333)).toMutableList()
), ),
RoadTripEntity( RoadTripEntity(
4, 4,
"Islande", "Islande",
Date(), Date(),
listOf(GeoPoint(48.866667, 2.333333), GeoPoint(48.866667, 2.333333), GeoPoint(48.866667, 2.333333)).toMutableList() listOf(Place(48.866667, 2.333333), Place(48.866667, 2.333333), Place(48.866667, 2.333333)).toMutableList()
), ),
) )
return list.toMutableList() return list.toMutableList()

@ -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 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( class Place(private val lat: Double,
val latitude: Double, private val lon: Double,
val longitude: 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()
}
}
}

@ -1,5 +1,6 @@
package uca.baptistearthur.geocaching.model package uca.baptistearthur.geocaching.model
import android.provider.Telephony.Mms.Addr
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@ -12,10 +13,10 @@ class RoadTripEntity(
@PrimaryKey(autoGenerate = true) val id: Int, @PrimaryKey(autoGenerate = true) val id: Int,
@ColumnInfo(name="name") val name: String, @ColumnInfo(name="name") val name: String,
@ColumnInfo(name="date") val date: Date, @ColumnInfo(name="date") val date: Date,
@ColumnInfo(name="places") val places: MutableList<GeoPoint> @ColumnInfo(name="places") val places: MutableList<Place>
){ ){
fun addPlaceToRoadTripList(place: GeoPoint) = places.add(place) fun addPlaceToRoadTripList(place: Place) = places.add(place)
fun addPlaceToRoadTripList(latitude: Double, longitude: Double) = places.add(GeoPoint(latitude, longitude)) fun addPlaceToRoadTripList(latitude: Double, longitude: Double) = places.add(Place(latitude, longitude))
fun toJSON(): String = Gson().toJson(this) fun toJSON(): String = Gson().toJson(this)
} }

@ -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)
}
}

@ -6,7 +6,6 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import org.osmdroid.util.GeoPoint import org.osmdroid.util.GeoPoint
import uca.baptistearthur.geocaching.R import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.Place
class PlacesAdapter (val places: List<GeoPoint>) : RecyclerView.Adapter<PlacesViewHolder>(){ class PlacesAdapter (val places: List<GeoPoint>) : RecyclerView.Adapter<PlacesViewHolder>(){

@ -2,12 +2,16 @@ package uca.baptistearthur.geocaching.ui.activity
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.coroutines.*
import uca.baptistearthur.geocaching.R import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.network.AddressAPI
import uca.baptistearthur.geocaching.network.AddressNetwork
class MainWindow: AppCompatActivity() { class MainWindow: AppCompatActivity() {

@ -1,38 +1,21 @@
package uca.baptistearthur.geocaching.ui.fragment package uca.baptistearthur.geocaching.ui.fragment
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import org.osmdroid.config.Configuration import org.osmdroid.config.Configuration
import org.osmdroid.util.GeoPoint import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.ScaleBarOverlay import org.osmdroid.views.overlay.ScaleBarOverlay
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.location.Location
import android.location.LocationListener
import android.util.Log import android.util.Log
import android.widget.ProgressBar
import androidx.activity.result.contract.ActivityResultContracts
import org.osmdroid.bonuspack.routing.OSRMRoadManager import org.osmdroid.bonuspack.routing.OSRMRoadManager
import org.osmdroid.bonuspack.routing.RoadManager
import org.osmdroid.config.IConfigurationProvider
import org.osmdroid.library.BuildConfig
import org.osmdroid.tileprovider.tilesource.TileSourceFactory import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.tileprovider.util.StorageUtils.getStorage
import org.osmdroid.views.overlay.compass.CompassOverlay import org.osmdroid.views.overlay.compass.CompassOverlay
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
import uca.baptistearthur.geocaching.R import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.ui.overlay.AddMarkerOverlay import uca.baptistearthur.geocaching.ui.overlay.AddMarkerOverlay
import uca.baptistearthur.geocaching.ui.overlay.RecenterOverlay
open class Map : Fragment() { open class Map : Fragment() {
protected lateinit var map : MapView protected lateinit var map : MapView
@ -71,7 +54,6 @@ open class Map : Fragment() {
// Add Marker Overlay // Add Marker Overlay
val addMarker = AddMarkerOverlay(OSRMRoadManager(context, userAgent)) val addMarker = AddMarkerOverlay(OSRMRoadManager(context, userAgent))
map.overlays.add(addMarker); map.overlays.add(addMarker);
addMarker.addPlaces(listOf(GeoPoint(50.5, 10.1), GeoPoint(52.5, 33.33), GeoPoint(66.0, 33.35)), map)
} }
override fun onCreateView( override fun onCreateView(

@ -14,7 +14,6 @@ import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Overlay import org.osmdroid.views.overlay.Overlay
import org.osmdroid.views.overlay.Polyline import org.osmdroid.views.overlay.Polyline
import uca.baptistearthur.geocaching.R import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.Place
class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() { class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {

@ -12,11 +12,15 @@ import android.view.MotionEvent
import android.widget.EditText import android.widget.EditText
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.osmdroid.util.GeoPoint import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Overlay import org.osmdroid.views.overlay.Overlay
import uca.baptistearthur.geocaching.R import uca.baptistearthur.geocaching.R
import uca.baptistearthur.geocaching.model.Address
import uca.baptistearthur.geocaching.model.Place import uca.baptistearthur.geocaching.model.Place
import uca.baptistearthur.geocaching.model.RoadTripEntity import uca.baptistearthur.geocaching.model.RoadTripEntity
import java.util.* import java.util.*
@ -76,7 +80,13 @@ class NewRoadtripOverlay(val points: Collection<PlaceMarker>) : Overlay() {
.setPositiveButton(R.string.confirm) { _, _ -> .setPositiveButton(R.string.confirm) { _, _ ->
val userInput = input.text.toString() val userInput = input.text.toString()
if (userInput.isNotBlank()) { if (userInput.isNotBlank()) {
val places: MutableList<GeoPoint> = points.map { GeoPoint(it.position.latitude, it.position.longitude) }.toMutableList() val places: MutableList<Place> = points.map { Place(it.position.latitude, it.position.longitude) }.toMutableList()
CoroutineScope(Dispatchers.Main).launch {
places.forEach{
it.initAddress()
Log.d("GeoMap", it.address.displayName)
}
}
val newRoadTrip = RoadTripEntity( val newRoadTrip = RoadTripEntity(
id = 0, // auto-generated ID id = 0, // auto-generated ID
name = input.text.toString(), name = input.text.toString(),

@ -29,9 +29,11 @@
android:textColor="@color/main_turquoise_50" android:textColor="@color/main_turquoise_50"
android:textSize="15sp" android:textSize="15sp"
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="5dp"/> android:paddingBottom="5dp"/>
<org.osmdroid.views.MapView <androidx.fragment.app.FragmentContainerView
android:name="uca.baptistearthur.geocaching.ui.fragment.Map"
android:id="@+id/roadTripDetailMapView" android:id="@+id/roadTripDetailMapView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"

Loading…
Cancel
Save