Skip to content
Snippets Groups Projects
Commit 546e0bfe authored by Ignacio Hernández de la Fuente's avatar Ignacio Hernández de la Fuente
Browse files

stuff

parent 770e5afb
Branches map
No related tags found
No related merge requests found
...@@ -38,6 +38,7 @@ android { ...@@ -38,6 +38,7 @@ android {
dependencies { dependencies {
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1' implementation 'com.google.android.material:material:1.6.1'
......
...@@ -16,6 +16,7 @@ import com.google.android.gms.maps.model.MarkerOptions ...@@ -16,6 +16,7 @@ import com.google.android.gms.maps.model.MarkerOptions
import com.example.googlemapsapp.databinding.ActivityMapsBinding import com.example.googlemapsapp.databinding.ActivityMapsBinding
import com.google.android.gms.location.FusedLocationProviderClient import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices import com.google.android.gms.location.LocationServices
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MapsActivity : AppCompatActivity(), OnMapReadyCallback { class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
...@@ -26,6 +27,10 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { ...@@ -26,6 +27,10 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private val permissionCode =101 private val permissionCode =101
private lateinit var fab: FloatingActionButton
private var isFabClicked = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
...@@ -34,10 +39,21 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { ...@@ -34,10 +39,21 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
fusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(this) 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(){ private fun getCurrentLocationUser(){
...@@ -58,9 +74,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { ...@@ -58,9 +74,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
Toast.makeText(applicationContext, currentLocation.latitude.toString()+""+currentLocation.longitude.toString(), Toast.makeText(applicationContext, currentLocation.latitude.toString()+""+currentLocation.longitude.toString(),
Toast.LENGTH_LONG).show() Toast.LENGTH_LONG).show()
val mapFragment = supportFragmentManager placeMarker(LatLng(currentLocation.latitude, currentLocation.longitude))
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
} }
} }
} }
...@@ -81,11 +95,17 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback { ...@@ -81,11 +95,17 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
} }
override fun onMapReady(googleMap: GoogleMap) { override fun onMapReady(googleMap: GoogleMap) {
val latLng =LatLng(currentLocation.latitude, currentLocation.longitude) // El mapa está listo para ser utilizado
val markerOptions =MarkerOptions().position(latLng).title("Current Location") mMap = googleMap
googleMap?.animateCamera(CameraUpdateFactory.newLatLng(latLng)) // Configura el listener de largo clic en el mapa
googleMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 7f)) mMap.setOnMapLongClickListener { latLng ->
googleMap?.addMarker(markerOptions) 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
<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>
<?xml version="1.0" encoding="utf-8"?> <?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:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<fragment
android:id="@+id/map" android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MapsActivity" /> android:layout_above="@+id/fab"
\ No newline at end of file />
<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>
<resources> <resources>
<string name="app_name">GoogleMapsApp</string> <string name="app_name">GoogleMapsApp</string>
<string name="title_activity_maps">MapsActivity</string> <string name="title_activity_maps">MapsActivity</string>
<string name="fab_description">Descripción del botón flotante</string>
</resources> </resources>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment