add bouton de recharche géocaliser avec l'api

master
Bastien OLLIER 2 years ago
parent 1f06a7c95b
commit da2c34ceac

@ -14,6 +14,8 @@ fun getRestaurants(lat: Double, lon: Double, radius: Int): List<RestaurantData>
try { try {
val response = apiInterface.getRestaurants(lat, lon, radius) val response = apiInterface.getRestaurants(lat, lon, radius)
Log.e("Debug", "${lat},${lon}")
Log.e("Debug", response.toString())
response.results.forEach { response.results.forEach {
listRestaurant += RestaurantData(it.position.lat,it.position.lon,it.poi.name,it.poi.phone,it.address.freeformAddress) listRestaurant += RestaurantData(it.position.lat,it.position.lon,it.poi.name,it.poi.phone,it.address.freeformAddress)
} }

@ -0,0 +1,56 @@
package fr.iut.mapping
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
class LocationListener(private val context: Context) : LocationListener {
var latitude: Double = 0.0
get() = field
var longitude: Double = 0.0
get() = field
private val locationManager: LocationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
companion object {
const val permission = Manifest.permission.ACCESS_FINE_LOCATION
}
override fun onLocationChanged(location: Location) {
latitude = location.latitude
longitude = location.longitude
}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
fun start() {
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0L,
0f,
this
)
}
}
fun stop() {
locationManager.removeUpdates(this)
}
}

@ -3,7 +3,6 @@ package fr.iut.mapping
import android.Manifest.permission.ACCESS_COARSE_LOCATION import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.location.Location
import android.os.Build import android.os.Build
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle

@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
@ -21,13 +22,14 @@ import com.mapbox.mapboxsdk.plugins.annotation.Symbol
import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager
import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions
import com.mapbox.mapboxsdk.utils.BitmapUtils import com.mapbox.mapboxsdk.utils.BitmapUtils
import fr.iut.mapping.API.RestaurantData
import fr.iut.mapping.API.getRestaurants import fr.iut.mapping.API.getRestaurants
import kotlinx.android.synthetic.main.fragment_map_page.* import kotlinx.android.synthetic.main.fragment_map_page.*
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
//https://maplibre.org/maplibre-gl-native/android/api/index.html //https://maplibre.org/maplibre-gl-native/android/api/index.html
class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener { class FirstFragment: Fragment(R.layout.fragment_map_page) {
companion object { companion object {
private const val MARKER_SELECTED_ICON = "JAWG_ICON" private const val MARKER_SELECTED_ICON = "JAWG_ICON"
private const val MARKER_ICON = "MARKER_ICON" private const val MARKER_ICON = "MARKER_ICON"
@ -37,6 +39,9 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
private var symbolManager: SymbolManager? = null private var symbolManager: SymbolManager? = null
private var lastSymbol: Symbol? = null private var lastSymbol: Symbol? = null
private lateinit var locationListener: fr.iut.mapping.LocationListener
private lateinit var listRestaurant: List<RestaurantData>
private fun makeStyleUrl(): String { private fun makeStyleUrl(): String {
return "${getString(R.string.mapbox_style_url)}"; return "${getString(R.string.mapbox_style_url)}";
} }
@ -45,14 +50,40 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val list = getRestaurants(51.5,-0.11,1000) locationListener = LocationListener(requireContext())
locationListener.start()
Mapbox.getInstance(requireContext(), R.string.mapbox_access_token.toString()) Mapbox.getInstance(requireContext(), R.string.mapbox_access_token.toString())
val rootView = inflater.inflate(R.layout.fragment_map_page, container, false) val rootView = inflater.inflate(R.layout.fragment_map_page, container, false)
val button: View = rootView.findViewById(R.id.findRestaurant)
button.setOnClickListener { view ->
findAndPutRestaurant()
}
mapView = rootView.findViewById(R.id.mapView) mapView = rootView.findViewById(R.id.mapView)
mapView?.onCreate(savedInstanceState) mapView?.onCreate(savedInstanceState)
mapView?.getMapAsync { map ->
map.setStyle(makeStyleUrl()) { style ->
map.uiSettings.setAttributionMargins(15, 0, 0, 15)
}
}
return rootView
}
private fun findAndPutRestaurant(){
Log.e("Debug","${locationListener.latitude},${locationListener.latitude}")
listRestaurant = getRestaurants(locationListener.latitude,locationListener.latitude,1000000)
if(listRestaurant.isEmpty()){
Toast.makeText(requireContext(), "Pas de restaurants trouver", Toast.LENGTH_SHORT).show()
}
mapView?.getMapAsync { map -> mapView?.getMapAsync { map ->
map.setStyle(makeStyleUrl()) { style -> map.setStyle(makeStyleUrl()) { style ->
map.uiSettings.setAttributionMargins(15, 0, 0, 15) map.uiSettings.setAttributionMargins(15, 0, 0, 15)
@ -67,12 +98,11 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
this.symbolManager?.iconAllowOverlap = true this.symbolManager?.iconAllowOverlap = true
this.symbolManager?.iconIgnorePlacement = true this.symbolManager?.iconIgnorePlacement = true
list.forEach { restaurant -> listRestaurant.forEach { restaurant ->
val description = "${restaurant.adress}\n${restaurant.phone ?: "Pas de numéro disponible"}\n${restaurant.lat},${restaurant.lon}" val description = "${restaurant.adress}${restaurant.phone ?: "Pas de numéro disponible"}\n${restaurant.lat},${restaurant.lon}"
insertIconOnMap( insertIconOnMap(
LatLng(restaurant.lat, restaurant.lon), LatLng(restaurant.lat, restaurant.lon),
restaurant.name, restaurant.name,
R.drawable.ic_menu_likes,
description description
) )
} }
@ -99,7 +129,6 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
} }
} }
return rootView
} }
private fun toggleLayout() { private fun toggleLayout() {
@ -124,7 +153,7 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
this.lastSymbol = symbol this.lastSymbol = symbol
} }
private fun insertIconOnMap(point: LatLng, title: String, imageId: Int, description: String) { private fun insertIconOnMap(point: LatLng, title: String, description: String) {
Log.e("Debug",title) Log.e("Debug",title)
// Convert datas of the marker into Json object. // Convert datas of the marker into Json object.
val jsonData = """ val jsonData = """
@ -169,11 +198,8 @@ class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mapView?.onDestroy() mapView?.onDestroy()
locationListener.stop()
} }
//methode pour le GPS
override fun onLocationChanged(location: Location) {
print("Latitude: " + location.latitude + " , Longitude: " + location.longitude)
}
} }

@ -2,63 +2,84 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"> xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
app:ignore="NamespaceTypo">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:weightSum="3"> android:weightSum="3"
app:layout_editor_absoluteX="88dp"
app:layout_editor_absoluteY="-76dp">
<LinearLayout <LinearLayout
android:id="@+id/mapLayout" android:id="@+id/mapLayout"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="3"> android:layout_weight="3">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="51.5"
mapbox:mapbox_cameraTargetLng="-0.11"
mapbox:mapbox_cameraZoom="11"
mapbox:mapbox_uiLogo="false"
mapbox:mapbox_uiAttribution="true" />
</LinearLayout> <com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="51.5073509"
mapbox:mapbox_cameraTargetLng="-0.1277583"
mapbox:mapbox_cameraZoom="11"
mapbox:mapbox_uiAttribution="true"
mapbox:mapbox_uiLogo="false">
<LinearLayout </com.mapbox.mapboxsdk.maps.MapView>
android:id="@+id/descriptionLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0"
android:weightSum="3"
android:orientation="vertical">
<TextView
android:id="@+id/titleView" </LinearLayout>
android:layout_width="match_parent"
android:layout_height="0dp" <com.google.android.material.floatingactionbutton.FloatingActionButton
android:gravity="center" android:id="@+id/findRestaurant"
android:padding="10dp" android:layout_width="wrap_content"
android:textSize="30dp" android:layout_height="wrap_content"
android:text=" " android:layout_gravity="end|bottom"
android:textAlignment="center" android:layout_margin="16dp"
android:layout_weight="1" /> android:src="@android:drawable/ic_menu_search" />
<TextView
android:id="@+id/descriptionView"
android:layout_width="match_parent"
<LinearLayout
android:id="@+id/descriptionLayout"
android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_height="0dp"
android:gravity="center" android:layout_weight="0"
android:padding="10dp" android:orientation="vertical"
android:textSize="15dp" android:weightSum="3">
android:text=" "
android:textAlignment="center"
android:layout_weight="2" /> <TextView
android:id="@+id/titleView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text=" "
android:textAlignment="center"
android:textSize="30dp" />
<TextView
android:id="@+id/descriptionView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:padding="10dp"
android:text=" "
android:textAlignment="center"
android:textSize="15dp" />
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save