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 --- app/src/main/AndroidManifest.xml | 3 + .../features/location/FakeLocationFragment.kt | 221 ++++++++++++++++++--- .../features/location/FakeLocationMapView.kt | 45 +++++ app/src/main/res/drawable/ic_map_marker_blue.xml | 11 + app/src/main/res/drawable/ic_map_marker_red.xml | 28 +++ app/src/main/res/layout/fragment_fake_location.xml | 12 +- app/src/main/res/values/strings.xml | 2 + 7 files changed, 292 insertions(+), 30 deletions(-) create mode 100644 app/src/main/java/foundation/e/privacycentralapp/features/location/FakeLocationMapView.kt create mode 100644 app/src/main/res/drawable/ic_map_marker_blue.xml create mode 100644 app/src/main/res/drawable/ic_map_marker_red.xml (limited to 'app/src') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2c3b055..74c226c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,9 @@ + + + { + 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