Skip to content

Commit

Permalink
[RFR-614] Disable auto-zoom on marker map (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 authored Jun 23, 2023
1 parent 114618a commit 98ab68a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.util.Calendar
* The [Fragment] which shows a map with markers to the user.
*
* @author Armin Schnabel
* @version 1.0.0
* @version 1.0.1
* @since 3.2.1
*/
class MarkerFragment : Fragment() {
Expand Down Expand Up @@ -109,7 +109,7 @@ class MarkerFragment : Fragment() {
_binding = FragmentMarkerBinding.inflate(inflater, container, false)
val root: View = binding.root

map = Map(binding.mapView, savedInstanceState, onMapReadyRunnable, permissionLauncher)
map = Map(binding.mapView, savedInstanceState, onMapReadyRunnable, permissionLauncher, true)

return root
}
Expand Down
50 changes: 27 additions & 23 deletions utils/src/main/kotlin/de/cyface/app/utils/Map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ import java.lang.ref.WeakReference
* The Map class handles everything around the GoogleMap view.
*
* @author Armin Schnabel
* @version 4.0.0
* @version 4.1.0
* @since 1.0.0
* @property view The `MapView` element of the `GoogleMap`.
* @property onMapReadyRunnable The `Runnable` triggered when the `GoogleMap` is loaded and ready.
* @property ignoreAutoZoom `true` if the map should ignore the user preferences and never activate
* auto-zoom. `false` if auto-zoom should be enabled depending on the user preferences.
*/
class Map(
private val view: MapView,
savedInstanceState: Bundle?,
onMapReadyRunnable: Runnable,
permissionLauncher: ActivityResultLauncher<Array<String>>
permissionLauncher: ActivityResultLauncher<Array<String>>,
private val ignoreAutoZoom: Boolean = false
) : OnMapReadyCallback, LocationListener {
/**
* The visualization library in use.
Expand Down Expand Up @@ -150,11 +153,12 @@ class Map(
private fun requestLocationUpdates() {
val activity = view.context as Activity
fusedLocationClient = LocationServices.getFusedLocationProviderClient(activity)
val locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 5_000).build()
val locationRequest =
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 5_000).build()
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
// The GMap location does not work on emulator, see bug report: https://issuetracker.google.com/issues/242438611
if (locationResult.locations.size > 0 && isAutoCenterMapEnabled) {
if (locationResult.locations.size > 0 && isAutoCenterMapEnabled && !ignoreAutoZoom) {
moveToLocation(
true,
locationResult.locations[locationResult.locations.size - 1]
Expand Down Expand Up @@ -447,7 +451,7 @@ class Map(

override fun onLocationChanged(location: Location) {
// This is used by `ui/cyface`, the `ui/r4r` uses `onLocationResult`
if (isAutoCenterMapEnabled) {
if (isAutoCenterMapEnabled && !ignoreAutoZoom) {
moveToLocation(true, location)
}
}
Expand Down Expand Up @@ -476,32 +480,32 @@ class Map(
* @param isMarkerToBeFocused `True` if the newly added `Marker` is to be focused after creation
* /
fun addMarker(
eventId: Long, latLng: LatLng, modality: Modality,
isMarkerToBeFocused: Boolean
eventId: Long, latLng: LatLng, modality: Modality,
isMarkerToBeFocused: Boolean
) {
val modalityKey = applicationContext.getString(R.string.modality_type)
val modalityValue = getTranslation(WeakReference(applicationContext), modality)
val markerTitle = "$modalityKey : $modalityValue"
val markerOptions =
MarkerOptions().position(LatLng(latLng.latitude, latLng.longitude)).title(markerTitle)
val marker = googleMap!!.addMarker(markerOptions)
eventMarker[eventId] = marker
if (isMarkerToBeFocused) {
focusMarker(marker!!)
}
val modalityKey = applicationContext.getString(R.string.modality_type)
val modalityValue = getTranslation(WeakReference(applicationContext), modality)
val markerTitle = "$modalityKey : $modalityValue"
val markerOptions =
MarkerOptions().position(LatLng(latLng.latitude, latLng.longitude)).title(markerTitle)
val marker = googleMap!!.addMarker(markerOptions)
eventMarker[eventId] = marker
if (isMarkerToBeFocused) {
focusMarker(marker!!)
}
}
/**
* Moves the camera of the map to the {@param marker} position and shows the Marker title.
*
* @param marker The `Marker` which is to be focused.
*/
*/
fun focusMarker(marker: Marker) {
marker.showInfoWindow()
val markerLatLng = marker.position
val googleMap = googleMap
val cameraUpdate = CameraUpdateFactory.newLatLng(markerLatLng)
googleMap!!.moveCamera(cameraUpdate)
marker.showInfoWindow()
val markerLatLng = marker.position
val googleMap = googleMap
val cameraUpdate = CameraUpdateFactory.newLatLng(markerLatLng)
googleMap!!.moveCamera(cameraUpdate)
}*/

/**
Expand Down

0 comments on commit 98ab68a

Please sign in to comment.