parent
f555769438
commit
55bf1f8952
@ -0,0 +1,31 @@
|
||||
package fr.iut.mapping.API
|
||||
|
||||
data class NearbyPOI(
|
||||
val results: List<Result>
|
||||
)
|
||||
|
||||
data class Result(
|
||||
val type: String,
|
||||
val id: String,
|
||||
val score: Double,
|
||||
val dist: Double,
|
||||
val info: String,
|
||||
val poi: POI,
|
||||
val address: Address,
|
||||
val position: Position,
|
||||
)
|
||||
|
||||
data class POI(
|
||||
val name: String,
|
||||
val phone: String?,
|
||||
)
|
||||
|
||||
data class Address(
|
||||
val freeformAddress: String,
|
||||
val localName: String
|
||||
)
|
||||
|
||||
data class Position(
|
||||
val lat: Double,
|
||||
val lon: Double
|
||||
)
|
@ -0,0 +1,30 @@
|
||||
package fr.iut.mapping.API
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
import retrofit2.http.Query
|
||||
|
||||
private const val TOMTOM_API_PATH = "https://api.tomtom.com/search/2/";
|
||||
val httpClient = OkHttpClient()
|
||||
|
||||
|
||||
interface RestaurantAPI {
|
||||
|
||||
@GET("nearbySearch/.json?categorySet=7315&key=s6oXlFd9IRM2vgPhNmzJ24HS2hlqcu00")
|
||||
suspend fun getRestaurants(@Query("lat") lat: Double, @Query("lon") lon: Double, @Query("radius") radius: Int): NearbyPOI
|
||||
|
||||
}
|
||||
|
||||
fun createTomtomConnection(): Retrofit =
|
||||
Retrofit.Builder()
|
||||
.baseUrl(TOMTOM_API_PATH)
|
||||
.addConverterFactory(MoshiConverterFactory.create(
|
||||
Moshi.Builder()
|
||||
.add(KotlinJsonAdapterFactory())
|
||||
.build()))
|
||||
.client(httpClient)
|
||||
.build()
|
@ -0,0 +1,10 @@
|
||||
package fr.iut.mapping.API
|
||||
|
||||
import com.squareup.moshi.Json
|
||||
|
||||
data class RestaurantData(val lat : Double,
|
||||
val lon : Double,
|
||||
val name: String,
|
||||
val phone:String?,
|
||||
val adress:String?) {
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package fr.iut.mapping.API
|
||||
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
|
||||
fun getRestaurants(lat: Double, lon: Double, radius: Int): List<RestaurantData> {
|
||||
var listRestaurant : List<RestaurantData> = emptyList()
|
||||
runBlocking {
|
||||
coroutineScope {
|
||||
var retrofit = createTomtomConnection()
|
||||
var apiInterface = retrofit.create(RestaurantAPI::class.java)
|
||||
|
||||
try {
|
||||
val response = apiInterface.getRestaurants(lat, lon, radius)
|
||||
response.results.forEach {
|
||||
listRestaurant += RestaurantData(it.position.lat,it.position.lon,it.poi.name,it.poi.phone,it.address.freeformAddress)
|
||||
}
|
||||
|
||||
} catch (Ex: Exception) {
|
||||
Log.e("Error", Ex.localizedMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return listRestaurant
|
||||
}
|
Loading…
Reference in new issue