Created
March 19, 2023 17:05
-
-
Save webserveis/767182d131da1b83f2ff5693c224e222 to your computer and use it in GitHub Desktop.
MapActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codelaby.mapviewer | |
import android.Manifest | |
import android.content.Context | |
import android.content.pm.PackageManager | |
import android.location.Location | |
import android.location.LocationListener | |
import android.location.LocationManager | |
import android.os.Build | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.ViewGroup | |
import android.widget.Toast | |
import androidx.activity.result.contract.ActivityResultContracts | |
import androidx.activity.viewModels | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.app.ActivityCompat | |
import androidx.core.content.res.ResourcesCompat | |
import androidx.core.view.ViewCompat | |
import androidx.core.view.WindowInsetsCompat | |
import androidx.lifecycle.Observer | |
import com.codelaby.mapviewer.databinding.ActivityMapViewerBinding | |
import com.google.android.gms.maps.* | |
import com.google.android.gms.maps.model.LatLng | |
import com.google.android.gms.maps.model.MarkerOptions | |
class MapViewerActivity : AppCompatActivity(), OnMapReadyCallback { | |
companion object { | |
private const val TAG = "MapViewerActivity" | |
} | |
private lateinit var insets: androidx.core.graphics.Insets | |
private lateinit var mMap: GoogleMap | |
private lateinit var binding: ActivityMapViewerBinding | |
private val locationViewModel: LocationViewModel by viewModels() | |
private val locationManager: LocationManager by lazy { | |
this.getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
} | |
private val locationListener = object : LocationListener { | |
override fun onLocationChanged(location: Location) { | |
// Manejar la nueva ubicación | |
Log.d(TAG, "onLocationChanged: " + location) | |
} | |
override fun onProviderEnabled(provider: String) { | |
if (provider == LocationManager.GPS_PROVIDER) { | |
// El proveedor de ubicación GPS ahora está disponible | |
Toast.makeText(this@MapViewerActivity, "GPS enabled", Toast.LENGTH_SHORT).show() | |
} | |
} | |
override fun onProviderDisabled(provider: String) { | |
if (provider == LocationManager.GPS_PROVIDER) { | |
// El proveedor de ubicación GPS ya no está disponible | |
Toast.makeText(this@MapViewerActivity, "GPS disabled", Toast.LENGTH_SHORT).show() | |
} | |
} | |
} | |
private val locationPermissionRequest = registerForActivityResult( | |
ActivityResultContracts.RequestMultiplePermissions() | |
) { permissions -> | |
when { | |
permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> { | |
// Precise location access granted. | |
Toast.makeText(this, "Precise location access granted", Toast.LENGTH_SHORT).show() | |
} | |
permissions.getOrDefault(android.Manifest.permission.ACCESS_COARSE_LOCATION, false) -> { | |
// Only approximate location access granted. | |
Toast.makeText(this, "Only approximate location access granted", Toast.LENGTH_SHORT).show() | |
} | |
else -> { | |
// No location access granted. | |
Toast.makeText(this, "No location access granted", Toast.LENGTH_SHORT).show() | |
} | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
MapsInitializer.initialize(this, MapsInitializer.Renderer.LATEST) { | |
//println(it.name) | |
Log.d(TAG, "map renderer: " + it.name) | |
} | |
binding = ActivityMapViewerBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
setSupportActionBar(binding.toolbar) | |
initWindowsInsets() | |
// Obtain the SupportMapFragment and get notified when the map is ready to be used. | |
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment | |
mapFragment.getMapAsync(this) | |
locationViewModel.getLocationData().observe(this, Observer { | |
Log.d(TAG, "onLocationUpdate: " + it) | |
}) | |
} | |
override fun onSupportNavigateUp(): Boolean { | |
onBackPressedDispatcher.onBackPressed() | |
return super.onSupportNavigateUp() | |
} | |
override fun onResume() { | |
super.onResume() | |
subscribeLocationTracking() | |
} | |
override fun onPause() { | |
super.onPause() | |
unSubscribeLocationTracking() | |
} | |
private fun initWindowsInsets() { | |
//Setting top margin toolbar | |
ViewCompat.setOnApplyWindowInsetsListener(binding.safeArea) { view, windowInsets -> | |
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) | |
with((view.layoutParams as ViewGroup.MarginLayoutParams)) { | |
leftMargin = insets.left | |
topMargin = insets.top | |
rightMargin = insets.right | |
bottomMargin = insets.bottom | |
} | |
this.insets = insets | |
WindowInsetsCompat.CONSUMED | |
} | |
} | |
override fun onMapReady(googleMap: GoogleMap) { | |
mMap = googleMap | |
setupGoogleMapView() | |
// Add a marker in Sydney and move the camera | |
val sydney = LatLng(-34.0, 151.0) | |
mMap.apply { | |
addMarker(MarkerOptions().position(sydney).title("Marker in Sydney")) | |
moveCamera(CameraUpdateFactory.newLatLng(sydney)) | |
} | |
} | |
private fun setupGoogleMapView() { | |
mMap.apply { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | |
mMap.setPadding(insets.left, insets.top, insets.right, insets.bottom) | |
} | |
uiSettings.apply { | |
isCompassEnabled = false | |
isMapToolbarEnabled = false | |
isMyLocationButtonEnabled = false | |
isRotateGesturesEnabled = false | |
isTiltGesturesEnabled = false | |
isZoomControlsEnabled = false | |
} | |
} | |
} | |
private fun subscribeLocationTracking() { | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( | |
this, | |
Manifest.permission.ACCESS_COARSE_LOCATION | |
) != PackageManager.PERMISSION_GRANTED | |
) { | |
locationPermissionRequest.launch( | |
arrayOf( | |
Manifest.permission.ACCESS_FINE_LOCATION, | |
android.Manifest.permission.ACCESS_COARSE_LOCATION | |
) | |
) | |
binding.fabMyLocation.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_round_location_disabled_24, theme)) | |
return | |
} | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10f, locationListener) | |
} | |
private fun unSubscribeLocationTracking() { | |
locationManager.removeUpdates(locationListener) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment