diff --git a/app/build.gradle b/app/build.gradle index 6835f3ef2167817bb5df270e137731a0c112bd83..64917fedb549cf382efe6ac4a4bbdb3d4e748121 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,6 +38,7 @@ android { dependencies { + implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.6.1' diff --git a/app/src/main/java/com/example/googlemapsapp/MapsActivity.kt b/app/src/main/java/com/example/googlemapsapp/MapsActivity.kt index 3b9fc5398fffab8780de2d0dca61c5e5d4892300..0853e5950948a66a3c17ebfe16e3c26918b5c274 100644 --- a/app/src/main/java/com/example/googlemapsapp/MapsActivity.kt +++ b/app/src/main/java/com/example/googlemapsapp/MapsActivity.kt @@ -16,6 +16,7 @@ import com.google.android.gms.maps.model.MarkerOptions import com.example.googlemapsapp.databinding.ActivityMapsBinding import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.LocationServices +import com.google.android.material.floatingactionbutton.FloatingActionButton class MapsActivity : AppCompatActivity(), OnMapReadyCallback { @@ -26,6 +27,10 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { private lateinit var fusedLocationProviderClient: FusedLocationProviderClient private val permissionCode =101 + private lateinit var fab: FloatingActionButton + + private var isFabClicked = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -34,10 +39,21 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { fusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(this) - // Obtain the SupportMapFragment and get notified when the map is ready to be used. + fab = findViewById(R.id.fab) + fab.setOnClickListener { + isFabClicked = true + Toast.makeText(applicationContext, "Haz clic en el mapa para colocar un marcador", Toast.LENGTH_LONG).show() + } + val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment + mapFragment.getMapAsync(this) + } - getCurrentLocationUser() + private fun placeMarker(latLng: LatLng) { + // Coloca un marcador en la ubicación proporcionada + mMap.addMarker(MarkerOptions().position(latLng).title("Marcador Personalizado")) + + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f)) } private fun getCurrentLocationUser(){ @@ -58,9 +74,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { Toast.makeText(applicationContext, currentLocation.latitude.toString()+""+currentLocation.longitude.toString(), Toast.LENGTH_LONG).show() - val mapFragment = supportFragmentManager - .findFragmentById(R.id.map) as SupportMapFragment - mapFragment.getMapAsync(this) + placeMarker(LatLng(currentLocation.latitude, currentLocation.longitude)) } } } @@ -81,11 +95,17 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { } override fun onMapReady(googleMap: GoogleMap) { - val latLng =LatLng(currentLocation.latitude, currentLocation.longitude) - val markerOptions =MarkerOptions().position(latLng).title("Current Location") - - googleMap?.animateCamera(CameraUpdateFactory.newLatLng(latLng)) - googleMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 7f)) - googleMap?.addMarker(markerOptions) + // El mapa está listo para ser utilizado + mMap = googleMap + + // Configura el listener de largo clic en el mapa + mMap.setOnMapLongClickListener { latLng -> + if (isFabClicked) { + // Se llama cuando el usuario realiza un largo clic en el mapa después de presionar el botón flotante + placeMarker(latLng) + } + isFabClicked = false + } + getCurrentLocationUser() } } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_add_marker.xml b/app/src/main/res/drawable/ic_add_marker.xml new file mode 100644 index 0000000000000000000000000000000000000000..9eb56cd5f8387848f5d783643924759f48df30af --- /dev/null +++ b/app/src/main/res/drawable/ic_add_marker.xml @@ -0,0 +1,7 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24.0" + android:viewportHeight="24.0"> + <!-- Add your vector drawable content here --> +</vector> diff --git a/app/src/main/res/layout/activity_maps.xml b/app/src/main/res/layout/activity_maps.xml index ec352fc6b1aa00e0353cac5e2f4c176086cc6900..8182f23813a55dc5b47035ec38f82915ff49a2e6 100644 --- a/app/src/main/res/layout/activity_maps.xml +++ b/app/src/main/res/layout/activity_maps.xml @@ -1,9 +1,27 @@ <?xml version="1.0" encoding="utf-8"?> -<fragment xmlns:android="http://schemas.android.com/apk/res/android" +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" - android:id="@+id/map" - android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MapsActivity" /> \ No newline at end of file + tools:context=".MapsActivity"> + + <fragment + android:id="@+id/map" + android:name="com.google.android.gms.maps.SupportMapFragment" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_above="@+id/fab" + /> + + <com.google.android.material.floatingactionbutton.FloatingActionButton + android:id="@+id/fab" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_alignParentBottom="true" + android:layout_alignParentEnd="true" + android:layout_margin="16dp" + android:src="@android:drawable/ic_dialog_info" + android:contentDescription="@string/fab_description" /> +</RelativeLayout> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ef6e33e3f9fd82fd3b04515201a1aa8ecba415a1..4b51e3f4a31a10251cc89604275e5c39887384d0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,5 @@ <resources> <string name="app_name">GoogleMapsApp</string> <string name="title_activity_maps">MapsActivity</string> + <string name="fab_description">Descripción del botón flotante</string> </resources> \ No newline at end of file