diff --git a/.gitignore b/.gitignore index 37f2a85..c9085c9 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,8 @@ captures/ .idea/dictionaries .idea/libraries .idea/caches +.idea/misc.xml +.idea/modules.xml # Keystore files # Uncomment the following line if you do not want to check your keystore files in. diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 45b5654..88ea3aa 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,8 +1,5 @@ - - diff --git a/.idea/misc.xml b/.idea/misc.xml index 37a7509..af0bbdd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,10 @@ + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml index dc3ef26..3d11077 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/app/build.gradle b/app/build.gradle index 3fa4bbf..3b95b39 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,8 +43,14 @@ dependencies { implementation "org.koin:koin-android-viewmodel:$koin_version" implementation 'com.android.support:recyclerview-v7:28.0.0' - implementation 'com.google.android.gms:play-services-maps:16.1.0' - implementation 'com.google.android.gms:play-services-location:11.8.0' + + implementation 'com.google.android.gms:play-services-maps:17.0.0' + implementation 'com.google.android.gms:play-services-gcm:17.0.0' + implementation 'com.google.android.gms:play-services-location:17.0.0' + + implementation 'com.google.firebase:firebase-firestore:21.2.0' + implementation 'com.google.firebase:firebase-core:17.2.0' + implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0' implementation 'com.google.firebase:firebase-firestore:21.2.0' implementation 'com.google.firebase:firebase-core:17.2.0' diff --git a/app/src/main/java/com/discordtime/gpts/maps/view/MapViewFragment.kt b/app/src/main/java/com/discordtime/gpts/maps/view/MapViewFragment.kt index e666094..e465109 100644 --- a/app/src/main/java/com/discordtime/gpts/maps/view/MapViewFragment.kt +++ b/app/src/main/java/com/discordtime/gpts/maps/view/MapViewFragment.kt @@ -1,34 +1,34 @@ package com.discordtime.gpts.maps.view -import android.annotation.SuppressLint import android.content.Context -import android.graphics.Bitmap -import android.graphics.Canvas +import android.location.Location import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.lifecycle.Observer -import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.ViewModelProviders import com.discordtime.gpts.R +import com.discordtime.gpts.maps.viewmodel.LocationListener +import com.discordtime.gpts.maps.viewmodel.LocationManager import com.discordtime.gpts.maps.viewmodel.MapViewModel +import com.discordtime.gpts.tools.Constants +import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.MapView import com.google.android.gms.maps.OnMapReadyCallback -import com.google.android.gms.maps.CameraUpdateFactory -import com.google.android.gms.maps.model.* -import com.google.firebase.FirebaseApp +import com.google.android.gms.maps.model.CameraPosition +import com.google.android.gms.maps.model.LatLng import org.koin.android.viewmodel.ext.android.viewModel -class MapViewFragment : Fragment(), OnMapReadyCallback { +class MapViewFragment : Fragment(), OnMapReadyCallback, LocationListener { private val mapViewModel: MapViewModel by viewModel() + private lateinit var mContext: Context private lateinit var mMapView: MapView - private lateinit var googleMap: GoogleMap + private lateinit var mGoogleMap: GoogleMap + private lateinit var mLocationManager: LocationManager override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_maps, container, false) @@ -41,14 +41,24 @@ class MapViewFragment : Fragment(), OnMapReadyCallback { mMapView.onCreate(savedInstanceState) mMapView.onResume() mMapView.getMapAsync(this) + + mLocationManager = LocationManager(mContext, this) + mLocationManager.startLocationUpdates() + } + + override fun onAttach(context: Context) { + super.onAttach(context) + mContext = context } - @SuppressLint("MissingPermission") override fun onMapReady(gMap: GoogleMap) { - googleMap = gMap - googleMap.isMyLocationEnabled + mGoogleMap = gMap + mGoogleMap.isMyLocationEnabled = true //enables location button (blue) + mGoogleMap.uiSettings.isCompassEnabled = false + mGoogleMap.uiSettings.isRotateGesturesEnabled = false + mGoogleMap.uiSettings.isTiltGesturesEnabled = false - // test + //test mapViewModel.getMarkedPlaces().observe(this, Observer { it.forEach { place -> @@ -56,12 +66,17 @@ class MapViewFragment : Fragment(), OnMapReadyCallback { mapViewModel.addMarker(place, gMap, view!!.context) } }) + } - // For dropping a marker at a point on the Map - val recife = LatLng(-8.055747, -34.871044) + override fun onLocationChanged(location: Location) { + val currentLocation = LatLng(location.latitude, location.longitude) // For zooming automatically to the location of the marker - val cameraPosition = CameraPosition.Builder().target(recife).zoom(12f).build() - googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)) + val cameraPosition = CameraPosition.Builder() + .target(currentLocation) + .zoom(Constants.CAMERA_MAPS_ZOOM) + .build() + + mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)) } } \ No newline at end of file diff --git a/app/src/main/java/com/discordtime/gpts/maps/viewmodel/LocationListener.kt b/app/src/main/java/com/discordtime/gpts/maps/viewmodel/LocationListener.kt new file mode 100644 index 0000000..83266d6 --- /dev/null +++ b/app/src/main/java/com/discordtime/gpts/maps/viewmodel/LocationListener.kt @@ -0,0 +1,8 @@ +package com.discordtime.gpts.maps.viewmodel + +import android.location.Location + +interface LocationListener { + + fun onLocationChanged(location: Location) +} \ No newline at end of file diff --git a/app/src/main/java/com/discordtime/gpts/maps/viewmodel/LocationManager.kt b/app/src/main/java/com/discordtime/gpts/maps/viewmodel/LocationManager.kt new file mode 100644 index 0000000..4f4303c --- /dev/null +++ b/app/src/main/java/com/discordtime/gpts/maps/viewmodel/LocationManager.kt @@ -0,0 +1,47 @@ +package com.discordtime.gpts.maps.viewmodel + +import android.content.Context +import android.location.Location +import android.os.Looper +import com.google.android.gms.location.* + +class LocationManager(val mContext: Context, private val locationListener: LocationListener) { + + private val UPDATE_INTERVAL: Long = 2 * 60 * 1000 //2 mins + private val FASTEST_INTERVAL: Long = 40 * 1000 //40 segs + + private val mLocationRequest = LocationRequest() + private lateinit var mFusedLocationProviderClient: FusedLocationProviderClient + + fun startLocationUpdates() { + //updates properties + mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY + mLocationRequest.interval = UPDATE_INTERVAL + mLocationRequest.fastestInterval = FASTEST_INTERVAL + + var builder = LocationSettingsRequest.Builder() + builder.addLocationRequest(mLocationRequest) + var locationSettingsRequest = builder.build() + + //checks if location settings are satisfied + var settingsClient = LocationServices.getSettingsClient(mContext) + settingsClient.checkLocationSettings(locationSettingsRequest) + + mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext) + mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, object : LocationCallback() { + override fun onLocationResult(locationResult: LocationResult?) { + onLocationChanged() + } + + }, Looper.myLooper()) + } + + private fun onLocationChanged() { + //var msg = "location changed: " + location?.latitude + ", " + location?.longitude + //Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show() + + mFusedLocationProviderClient.lastLocation.addOnSuccessListener { location: Location -> + locationListener.onLocationChanged(location) + } + } +} diff --git a/app/src/main/java/com/discordtime/gpts/tools/Constants.kt b/app/src/main/java/com/discordtime/gpts/tools/Constants.kt index bc54513..2baa34c 100644 --- a/app/src/main/java/com/discordtime/gpts/tools/Constants.kt +++ b/app/src/main/java/com/discordtime/gpts/tools/Constants.kt @@ -10,5 +10,8 @@ object Constants { //Value used for location permission const val PERMISSION_REQUEST_LOCATION_CODE = 99 + //maps camera default zoom + const val CAMERA_MAPS_ZOOM = 18.5f + const val PLACES_COLLECTION = "places" } \ No newline at end of file