Adding navigation between fragments and starting map configuration

pull/4/head
Arthur VALIN 2 years ago
parent 3a71233073
commit 7f6c10db75

@ -43,4 +43,5 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation "androidx.fragment:fragment-ktx:1.5.5"
implementation 'org.osmdroid:osmdroid-android:6.1.14'
}

@ -2,6 +2,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_A"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -12,36 +20,13 @@
android:supportsRtl="true"
android:theme="@style/Theme.Geocaching"
tools:targetApi="31" >
<!--
TODO: Before you run your application, you need a Google Maps API key.
To get one, follow the directions here:
https://developers.google.com/maps/documentation/android-sdk/get-api-key
Once you have your API key (it starts with "AIza"), define a new property in your
project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the
"YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />
<activity
android:name=".MainWindow"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

@ -0,0 +1,59 @@
package uca.baptistearthur.geocaching
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [List.newInstance] factory method to
* create an instance of this fragment.
*/
class List : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_list, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment List.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
List().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

@ -2,18 +2,41 @@ package uca.baptistearthur.geocaching
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import org.osmdroid.config.Configuration
class MainWindow: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Configuration.getInstance().userAgentValue = "Geocaching"
// Set the user agent for OsmDroid
setContentView(R.layout.main_window)
loadFragment(Map())
val navigation = findViewById<BottomNavigationView>(R.id.bottom_navigation)
navigation.selectedItemId= R.id.map
navigation.setOnItemSelectedListener {
when (it.itemId) {
R.id.profile -> {
loadFragment(Profile())
true
}
R.id.map -> {
loadFragment(Map())
true
}
R.id.list -> {
loadFragment(List())
true
}
else -> false
}
}
}
private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
}

@ -0,0 +1,100 @@
package uca.baptistearthur.geocaching
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import org.osmdroid.config.Configuration
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.ScaleBarOverlay
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.hardware.SensorManager
import android.location.Location
import android.location.LocationListener
import androidx.activity.result.contract.ActivityResultContracts
import org.osmdroid.views.overlay.compass.CompassOverlay
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
class Map : Fragment() {
private lateinit var map : MapView
val defaultPoint = GeoPoint(48.8583, 2.2944)
var isMapCentered = false;
val locationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
val geoPoint = GeoPoint(location.latitude, location.longitude)
if(!isMapCentered){
map.controller.setCenter(geoPoint)
isMapCentered=true;
}
map.invalidate()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Configuration.getInstance().userAgentValue = "Geocaching"
}
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission())
{ map.controller.setCenter(defaultPoint); }
private fun configureMap(view: View){
map = view.findViewById(R.id.mapView)
map.controller.setZoom(20.0);
if (ContextCompat.checkSelfPermission(requireActivity(), ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(ACCESS_FINE_LOCATION)
}
map.controller.setCenter(defaultPoint)
val compassOverlay = CompassOverlay(context, InternalCompassOrientationProvider(context), map);
compassOverlay.enableCompass();
map.getOverlays().add(compassOverlay);
val scaleBarOverlay = ScaleBarOverlay(map)
scaleBarOverlay.setAlignRight(true)
map.overlays.add(scaleBarOverlay)
val myLocation = MyLocationNewOverlay(GpsMyLocationProvider(context), map)
myLocation.enableFollowLocation()
myLocation.enableMyLocation()
map.overlays.add(myLocation)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_map, container, false)
configureMap(view)
return view
}
override fun onResume() {
super.onResume()
if (ContextCompat.checkSelfPermission(requireActivity(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, locationListener)
}
map.onResume() //needed for compass, my location overlays, v6.0.0 and up
}
override fun onPause() {
super.onPause()
if (ContextCompat.checkSelfPermission(requireActivity(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
locationManager.removeUpdates(locationListener)
isMapCentered=false;
}
map.onPause() //needed for compass, my location overlays, v6.0.0 and up
}
}

@ -0,0 +1,59 @@
package uca.baptistearthur.geocaching
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [Profile.newInstance] factory method to
* create an instance of this fragment.
*/
class Profile : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Profile.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
Profile().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".List">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="List" />
</FrameLayout>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Map">
<org.osmdroid.views.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true" />
</FrameLayout>

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Profile">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="PROFILE" />
</FrameLayout>

@ -18,13 +18,11 @@
android:textSize="20sp"
android:textStyle="bold" />
<org.osmdroid.views.MapView
android:id="@+id/mapView"
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:clickable="true"
android:focusable="true" />
android:layout_weight="8"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"

@ -3,4 +3,6 @@
<string name="profil">Profil</string>
<string name="carte">Carte</string>
<string name="liste">Liste</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
Loading…
Cancel
Save