Skip to content

Commit

Permalink
feature 25 - device location
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Olimpio committed Nov 5, 2019
1 parent 3887baa commit d889207
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
53 changes: 34 additions & 19 deletions app/src/main/java/com/discordtime/gpts/maps/view/MapViewFragment.kt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -41,27 +41,42 @@ 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 ->
Log.d("MapViewFragment","${place.name}")
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))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.discordtime.gpts.maps.viewmodel

import android.location.Location

interface LocationListener {

fun onLocationChanged(location: Location)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/discordtime/gpts/tools/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit d889207

Please sign in to comment.