|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package uca.baptistearthur.geocaching
|
|
|
|
|
package uca.baptistearthur.geocaching.ui.fragment
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
@ -14,18 +14,22 @@ 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 android.util.Log
|
|
|
|
|
import android.widget.ProgressBar
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
import uca.baptistearthur.geocaching.R
|
|
|
|
|
import uca.baptistearthur.geocaching.ui.overlay.RecenterOverlay
|
|
|
|
|
|
|
|
|
|
class Map : Fragment() {
|
|
|
|
|
private lateinit var map : MapView
|
|
|
|
|
private lateinit var spinner: ProgressBar;
|
|
|
|
|
private lateinit var locationManager: LocationManager;
|
|
|
|
|
val defaultPoint = GeoPoint(48.8583, 2.2944)
|
|
|
|
|
var isMapCentered = false;
|
|
|
|
|
val locationListener = object : LocationListener {
|
|
|
|
@ -33,12 +37,15 @@ class Map : Fragment() {
|
|
|
|
|
val geoPoint = GeoPoint(location.latitude, location.longitude)
|
|
|
|
|
if(!isMapCentered){
|
|
|
|
|
map.controller.setCenter(geoPoint)
|
|
|
|
|
spinner.visibility=View.GONE;
|
|
|
|
|
isMapCentered=true;
|
|
|
|
|
}
|
|
|
|
|
map.invalidate()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
Configuration.getInstance().userAgentValue = "Geocaching"
|
|
|
|
@ -46,11 +53,15 @@ class Map : Fragment() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission())
|
|
|
|
|
{ map.controller.setCenter(defaultPoint); }
|
|
|
|
|
{ map.controller.setCenter(defaultPoint) }
|
|
|
|
|
|
|
|
|
|
private fun configureMap(view: View){
|
|
|
|
|
Log.d("GeoMap", "MAP CONFIGURE")
|
|
|
|
|
map = view.findViewById(R.id.mapView)
|
|
|
|
|
map.controller.setZoom(20.0);
|
|
|
|
|
spinner = view.findViewById(R.id.mapLoading);
|
|
|
|
|
spinner.visibility=View.VISIBLE;
|
|
|
|
|
map.minZoomLevel = 10.0
|
|
|
|
|
map.controller.setZoom(21.0);
|
|
|
|
|
if (ContextCompat.checkSelfPermission(requireActivity(), ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
requestPermissionLauncher.launch(ACCESS_FINE_LOCATION)
|
|
|
|
|
}
|
|
|
|
@ -65,7 +76,6 @@ class Map : Fragment() {
|
|
|
|
|
map.overlays.add(scaleBarOverlay)
|
|
|
|
|
|
|
|
|
|
val myLocation = MyLocationNewOverlay(GpsMyLocationProvider(context), map)
|
|
|
|
|
myLocation.enableFollowLocation()
|
|
|
|
|
myLocation.enableMyLocation()
|
|
|
|
|
map.overlays.add(myLocation)
|
|
|
|
|
|
|
|
|
@ -79,6 +89,7 @@ class Map : Fragment() {
|
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
|
): View? {
|
|
|
|
|
Log.d("GeoMap", "MAP ON CREATE VIEW")
|
|
|
|
|
// Inflate the layout for this fragment
|
|
|
|
|
val view = inflater.inflate(R.layout.fragment_map, container, false)
|
|
|
|
|
configureMap(view)
|
|
|
|
@ -87,8 +98,9 @@ class Map : Fragment() {
|
|
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
|
super.onResume()
|
|
|
|
|
Log.d("GeoMap", "MAP RESUME")
|
|
|
|
|
if (ContextCompat.checkSelfPermission(requireActivity(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
val locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
|
|
|
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
|
|
|
|
@ -96,8 +108,9 @@ class Map : Fragment() {
|
|
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
|
super.onPause()
|
|
|
|
|
Log.d("GeoMap", "MAP PAUSE")
|
|
|
|
|
if (ContextCompat.checkSelfPermission(requireActivity(), ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
val locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
|
|
|
locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
|
|
|
locationManager.removeUpdates(locationListener)
|
|
|
|
|
isMapCentered=false;
|
|
|
|
|
}
|