From ca97c46120b3ce298bfd72bfeb208aa136076ea3 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 7 May 2021 00:10:39 +0530 Subject: Add mapview to pick location from map --- .../features/location/FakeLocationFragment.kt | 221 ++++++++++++++++++--- .../features/location/FakeLocationMapView.kt | 45 +++++ 2 files changed, 241 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationMapView.kt (limited to 'app/src/main/java/foundation/e') diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt b/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt index 5b58293..24ea426 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationFragment.kt @@ -17,31 +17,72 @@ package foundation.e.privacycentralapp.features.location +import android.annotation.SuppressLint +import android.content.Context import android.os.Bundle import android.text.Editable import android.util.Log +import android.view.Gravity import android.view.View +import android.view.ViewGroup import android.widget.Button +import android.widget.FrameLayout import android.widget.ImageView import android.widget.RadioButton import android.widget.Toast import android.widget.Toolbar +import androidx.annotation.NonNull +import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import com.google.android.material.textfield.TextInputLayout +import com.mapbox.android.core.permissions.PermissionsListener +import com.mapbox.android.core.permissions.PermissionsManager +import com.mapbox.mapboxsdk.Mapbox +import com.mapbox.mapboxsdk.geometry.LatLng +import com.mapbox.mapboxsdk.location.LocationComponent +import com.mapbox.mapboxsdk.location.LocationComponentActivationOptions +import com.mapbox.mapboxsdk.location.modes.CameraMode +import com.mapbox.mapboxsdk.location.modes.RenderMode +import com.mapbox.mapboxsdk.maps.MapboxMap +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback +import com.mapbox.mapboxsdk.maps.Style +import com.mapbox.mapboxsdk.style.layers.Property.NONE +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility +import com.mapbox.mapboxsdk.style.layers.SymbolLayer +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource import foundation.e.flowmvi.MVIView import foundation.e.privacycentralapp.R import foundation.e.privacycentralapp.dummy.LocationMode import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class FakeLocationFragment : Fragment(R.layout.fragment_fake_location), - MVIView { + MVIView, + PermissionsListener { + private lateinit var permissionsManager: PermissionsManager private val viewModel: FakeLocationViewModel by viewModels() + private lateinit var mapView: FakeLocationMapView + private lateinit var mapboxMap: MapboxMap + private var hoveringMarker: ImageView? = null + + private var mutableLatLongFlow = MutableStateFlow(LatLng()) + private var latLong = mutableLatLongFlow.asStateFlow() + + companion object { + private const val DROPPED_MARKER_LAYER_ID = "DROPPED_MARKER_LAYER_ID" + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) lifecycleScope.launchWhenStarted { @@ -62,6 +103,11 @@ class FakeLocationFragment : } } + override fun onAttach(context: Context) { + super.onAttach(context) + Mapbox.getInstance(requireContext(), getString(R.string.mapbox_key)) + } + private fun displayToast(message: String) { Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT) .show() @@ -71,10 +117,38 @@ class FakeLocationFragment : super.onViewCreated(view, savedInstanceState) val toolbar = view.findViewById(R.id.toolbar) setupToolbar(toolbar) + mapView = view.findViewById(R.id.mapView) + .setup(savedInstanceState) { mapboxMap -> + this.mapboxMap = mapboxMap + mapboxMap.setStyle(Style.MAPBOX_STREETS) { style -> + enableLocationPlugin(style) + + hoveringMarker = ImageView(requireContext()) + .apply { + setImageResource(R.drawable.mapbox_marker_icon_default) + val params = FrameLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER + ) + layoutParams = params + } + mapView.addView(hoveringMarker) + initDroppedMarker(style) + mapboxMap.addOnCameraMoveListener { + mutableLatLongFlow.value = mapboxMap.cameraPosition.target + } + mapboxMap.addOnCameraIdleListener { Log.d("Mapview", "camera move ended") } + } + } bindClickListeners(view) } private fun bindClickListeners(fragmentView: View) { + val latEditText = + fragmentView.findViewById(R.id.edittext_latitude).editText + val longEditText = + fragmentView.findViewById(R.id.edittext_longitude).editText + fragmentView.let { it.findViewById(R.id.radio_use_real_location) .setOnClickListener { radioButton -> @@ -90,15 +164,20 @@ class FakeLocationFragment : } it.findViewById