diff --git a/app/build.gradle b/app/build.gradle index a08ce67..e4b51c5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,16 +35,18 @@ android { } +//./gradlew build --refresh-dependencies dependencies { - implementation 'org.maplibre.gl:android-sdk:9.5.2' + //implementation ('org.maplibre.gl:android-sdk:9.5.2') + implementation ('com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0') + //implementation "com.mapbox.mapboxsdk:mapbox-android-accounts:0.7.0" - //implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.5.0' - implementation 'androidx.fragment:fragment-ktx:1.5.5' + implementation 'androidx.fragment:fragment-ktx:1.5.6' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' - implementation 'com.google.android.material:material:1.2.0' + implementation 'com.google.android.material:material:1.8.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 65a7334..f0df333 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ xmlns:tools="http://schemas.android.com/tools"> + + when { + permissions.getOrDefault(ACCESS_FINE_LOCATION, false) -> { + Toast.makeText(this, "Permission fine location Granted", Toast.LENGTH_SHORT).show() + } + permissions.getOrDefault(ACCESS_COARSE_LOCATION, false) -> { + Toast.makeText(this, "Permission COARSE location Granted", Toast.LENGTH_SHORT).show() + } else -> { + Toast.makeText(this, "Denied permission", Toast.LENGTH_SHORT).show() + } + } + } + locationPermissionRequest.launch(arrayOf(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION)) + /*navBar*/ val firstFragment=FirstFragment() val secondFragment=SecondFragment() @@ -28,9 +51,10 @@ class MainActivity : AppCompatActivity() { } - private fun setCurrentFragment(fragment: Fragment)= + private fun setCurrentFragment(fragment: Fragment) = supportFragmentManager.beginTransaction().apply { replace(R.id.flFragment,fragment) commit() } + } \ No newline at end of file diff --git a/app/src/main/java/fr/iut/mapping/MapPage.kt b/app/src/main/java/fr/iut/mapping/MapPage.kt index 74bc298..6f9ad9b 100644 --- a/app/src/main/java/fr/iut/mapping/MapPage.kt +++ b/app/src/main/java/fr/iut/mapping/MapPage.kt @@ -1,50 +1,138 @@ package fr.iut.mapping +import android.location.Location +import android.location.LocationListener +import android.os.Build import android.os.Bundle -import android.provider.Settings.Global.getString import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.Toast +import androidx.annotation.RequiresApi +import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.Fragment +import com.google.gson.JsonParser import com.mapbox.mapboxsdk.Mapbox import com.mapbox.mapboxsdk.geometry.LatLng import com.mapbox.mapboxsdk.maps.MapView -import com.mapbox.mapboxsdk.maps.MapboxMapOptions -import com.mapbox.mapboxsdk.maps.Style +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 kotlinx.android.synthetic.main.fragment_map_page.* + + +//https://maplibre.org/maplibre-gl-native/android/api/index.html +class FirstFragment: Fragment(R.layout.fragment_map_page), LocationListener { + companion object { + private const val MARKER_SELECTED_ICON = "JAWG_ICON" + private const val MARKER_ICON = "MARKER_ICON" + } -class FirstFragment:Fragment(R.layout.fragment_map_page) { private var mapView: MapView? = null + private var symbolManager: SymbolManager? = null + private var lastSymbol: Symbol? = null + + private val locationPermissionCode = 2 - // Returns the Jawg url depending on the style given (jawg-streets by default) - // See /res/values/strings which contains the url, the list of styles and your access token. private fun makeStyleUrl(): String { - print("${getString(R.string.mapbox_style_url)}") return "${getString(R.string.mapbox_style_url)}"; } + @RequiresApi(Build.VERSION_CODES.LOLLIPOP) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { super.onCreate(savedInstanceState) - Mapbox.getInstance(requireContext()) - // Inflate the layout for this fragment + Mapbox.getInstance(requireContext(), R.string.mapbox_access_token.toString()) val rootView = inflater.inflate(R.layout.fragment_map_page, container, false) - - // We get the map view to set its style with the desired Jawg url. mapView = rootView.findViewById(R.id.mapView) mapView?.onCreate(savedInstanceState) mapView?.getMapAsync { map -> - map.setStyle(makeStyleUrl()) { - // Map fully loaded in this scope. - // Update attributions position + map.setStyle(makeStyleUrl()) { style -> map.uiSettings.setAttributionMargins(15, 0, 0, 15) + val selectedMarkerIconDrawable = ResourcesCompat.getDrawable(this.resources, R.drawable.ic_menu_likes, null) + style.addImage(MARKER_ICON, BitmapUtils.getBitmapFromDrawable(selectedMarkerIconDrawable)!!) + val markerIconDrawable = ResourcesCompat.getDrawable(this.resources, R.drawable.ic_menu_likes, null) + style.addImage(MARKER_SELECTED_ICON, BitmapUtils.getBitmapFromDrawable(markerIconDrawable)!!) + + // Initialize SymbolManager. + this.symbolManager = SymbolManager(mapView!!, map, style) + this.symbolManager?.iconAllowOverlap = true + this.symbolManager?.iconIgnorePlacement = true + + // Insert markers with their associated data. + insertIconOnMap( + LatLng(51.50853, -0.076132), + "Tower of London", + R.drawable.ic_menu_likes, + "It is a historic castle on the north bank of the River Thames in central London." + + "It lies within the London Borough of Tower Hamlets, which is separated from the eastern edge of the square mile of the City of London by the open space known as Tower Hill." + + "It was founded towards the end of 1066 as part of the Norman Conquest of England." + ) + + // 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!!) + descriptionLayout.background.alpha = 30 + // Set the new marker as selected and toggle layout. + setSelectedIcon(it) + toggleLayout() + true + } } } return rootView } + private fun toggleLayout() { + mapView?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 2f) + descriptionLayout?.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f) + } + + private fun setDefaultIcon(symbol: Symbol) { + symbol.iconImage = MARKER_ICON + symbol.iconSize = 0.24f + symbolManager?.update(symbol) + } + + private fun setSelectedIcon(symbol: Symbol) { + symbol.iconImage = MARKER_SELECTED_ICON + symbol.iconSize = 0.25f + symbolManager?.update(symbol) + + if (this.lastSymbol != null) { + setDefaultIcon(this.lastSymbol!!) + } + this.lastSymbol = symbol + } + + private fun insertIconOnMap(point: LatLng, title: String, imageId: Int, description: String) { + // 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)) + ) + setDefaultIcon(newSymbol) + } + override fun onStart() { super.onStart() mapView?.onStart() @@ -74,4 +162,10 @@ class FirstFragment:Fragment(R.layout.fragment_map_page) { super.onDestroy() mapView?.onDestroy() } + + //methode pour le GPS + override fun onLocationChanged(location: Location) { + print("Latitude: " + location.latitude + " , Longitude: " + location.longitude) + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_map_page.xml b/app/src/main/res/layout/fragment_map_page.xml index fade03c..4d78f73 100644 --- a/app/src/main/res/layout/fragment_map_page.xml +++ b/app/src/main/res/layout/fragment_map_page.xml @@ -1,13 +1,64 @@ - + android:layout_height="match_parent" + xmlns:mapbox="http://schemas.android.com/apk/res-auto"> - - \ No newline at end of file + android:layout_height="match_parent" + android:orientation="vertical" + android:weightSum="3"> + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index c518bc1..9b60640 100644 --- a/build.gradle +++ b/build.gradle @@ -3,4 +3,4 @@ plugins { id 'com.android.application' version '7.3.0' apply false id 'com.android.library' version '7.3.0' apply false id 'org.jetbrains.kotlin.android' version '1.7.10' apply false -} \ No newline at end of file +} diff --git a/gradle.properties b/gradle.properties index c22da57..e832eaf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,5 +20,5 @@ kotlin.code.style=official # Enables namespacing of each library's R class so that its R class includes only the # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library -android.nonTransitiveRClass=true -android.enableJetifier=true \ No newline at end of file + +android.nonTransitiveRClass=true \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 1cf911f..23a0dca 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,6 +10,20 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + maven { + url 'https://api.mapbox.com/downloads/v2/releases/maven' + authentication { + basic(BasicAuthentication) + } + credentials { + // Do not change the username below. + // This should always be `mapbox` (not your username). + username = 'gorky1234' + // Use the secret token you stored in gradle.properties as the password + password = "pk.eyJ1IjoiZ29ya3kxMjM0IiwiYSI6ImNsZm1wY2w3ZTBkZ3EzcG1taXc5c29zYzQifQ.XZFV0wLqNLAgSgwY5C_SpQ" + } + } + jcenter() } } rootProject.name = "Mapping"