Skip to content

Commit

Permalink
feat: added gps location on the map
Browse files Browse the repository at this point in the history
  • Loading branch information
Polymeth committed Jun 2, 2024
1 parent a06b6d5 commit d77a709
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.github.se.assocify.ui.screens.event.maptab

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand All @@ -13,6 +18,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
Expand All @@ -30,11 +37,17 @@ import org.osmdroid.views.CustomZoomButtonsController
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.TilesOverlay
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay

// Initial position of the map (EPFL Agora)
private val INITIAL_POSITION = GeoPoint(46.518726, 6.566613)
// Initial zoom of the map (Zoom made to be focused on the EPFL campus)
private const val INITIAL_ZOOM = 17.0
// The location overlay to display the user's location
private var myLocationOverlay: MyLocationNewOverlay? = null
// Permission code for accessing GPS location
const val LOCATION_PERMISSION_REQUEST_CODE = 1000

/** A screen that displays a map of the event: location with the associated tasks. */
@Composable
Expand Down Expand Up @@ -93,6 +106,18 @@ fun rememberMapViewWithLifecycle(): MapView {
onDispose { lifecycle.removeObserver(lifecycleObserver) }
}

// Request location permissions and initialize myLocationOverlay
LaunchedEffect(Unit) {
val permissionGranted = requestLocationPermission(context)
if (permissionGranted) {
val myLocationProvider = GpsMyLocationProvider(context)
myLocationProvider.startLocationProvider(null)
myLocationOverlay = MyLocationNewOverlay(myLocationProvider, mapView)
myLocationOverlay?.enableMyLocation()
mapView.overlays.add(myLocationOverlay)
}
}

return mapView
}

Expand Down Expand Up @@ -154,6 +179,23 @@ fun EPFLMapView(
factory = { mapViewState }, modifier = modifier, update = { view -> onLoad?.invoke(view) })
}

/**
* Displays a permission dialog to request location permission
*
* @param context the context to display the dialog
*/
fun requestLocationPermission(context: Context): Boolean {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
context as Activity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_PERMISSION_REQUEST_CODE)
return false
}
return true
}

/**
* The custom tile source from the EPFL plan API.
*
Expand Down

0 comments on commit d77a709

Please sign in to comment.