add info dans la pop pup a partir de l'api

master
Bastien OLLIER 2 years ago
parent f555769438
commit 55bf1f8952

@ -41,6 +41,11 @@ dependencies {
implementation ('com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0')
//implementation "com.mapbox.mapboxsdk:mapbox-android-accounts:0.7.0"
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
implementation "com.squareup.moshi:moshi-kotlin:1.12.0"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2-native-mt'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0'
implementation 'androidx.fragment:fragment-ktx:1.5.6'
implementation 'androidx.appcompat:appcompat:1.6.1'

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

@ -4,6 +4,7 @@ import android.location.Location
import android.location.LocationListener
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -20,7 +21,9 @@ import com.mapbox.mapboxsdk.plugins.annotation.Symbol
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions
import com.mapbox.mapboxsdk.utils.BitmapUtils
import fr.iut.mapping.API.getRestaurants
import kotlinx.android.synthetic.main.fragment_map_page.*
import kotlinx.coroutines.runBlocking
//https://maplibre.org/maplibre-gl-native/android/api/index.html
@ -42,6 +45,8 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreate(savedInstanceState)
val list = getRestaurants(51.5,-0.11,1000)
Mapbox.getInstance(requireContext(), R.string.mapbox_access_token.toString())
val rootView = inflater.inflate(R.layout.fragment_map_page, container, false)
@ -62,20 +67,29 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
this.symbolManager?.iconAllowOverlap = true
this.symbolManager?.iconIgnorePlacement = true
list.forEach { restaurant ->
val description = "${restaurant.adress}\n${restaurant.phone ?: "Pas de numéro disponible"}\n${restaurant.lat},${restaurant.lon}"
insertIconOnMap(
LatLng(51.50853, -0.076132),
"Tower of London",
LatLng(restaurant.lat, restaurant.lon),
restaurant.name,
R.drawable.ic_menu_likes,
"It is a historic castle on the north bank of the River Thames in central London."
description
)
}
// Add a listener to trigger markers clicks.
this.symbolManager?.addClickListener {
// Put all marker information into the layout.
titleView.text = it.data?.asJsonObject?.get("title")?.asString
descriptionView.text = it.data?.asJsonObject?.get("description")?.asString
descriptionLayout.background = ContextCompat.getDrawable(requireContext(),it.data?.asJsonObject?.get("imageId")?.asInt!!)
it?.data?.asJsonObject?.get("imageId")?.asInt?.let { imageId ->
descriptionLayout.background = ContextCompat.getDrawable(requireContext(), imageId)
}
if (descriptionLayout != null && descriptionLayout.background != null) {
descriptionLayout.background.alpha = 30
}
// Set the new marker as selected and toggle layout.
setSelectedIcon(it)
toggleLayout()
@ -111,17 +125,17 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
}
private fun insertIconOnMap(point: LatLng, title: String, imageId: Int, description: String) {
Log.e("Debug",title)
// Convert datas of the marker into Json object.
val jsonData = """
{
"title" : "$title",
"imageId" : "$imageId",
"description" : "$description"
}
"""
// Add symbol at specified lat/lon.
val newSymbol = symbolManager!!.create(
SymbolOptions().withLatLng(LatLng(point.latitude, point.longitude)).withData(JsonParser.parseString(jsonData))
SymbolOptions().withIconSize(1000.0F).withLatLng(LatLng(point.latitude, point.longitude)).withData(JsonParser.parseString(jsonData))
)
setDefaultIcon(newSymbol)
}

Loading…
Cancel
Save