Get the actual location and update it
This commit is contained in:
parent
d66ecc43b7
commit
859fd1d5ec
@ -37,6 +37,7 @@ dependencies {
|
|||||||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||||||
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||||
implementation 'com.google.android.material:material:1.2.0-alpha06'
|
implementation 'com.google.android.material:material:1.2.0-alpha06'
|
||||||
|
implementation "com.google.android.gms:play-services-location:17.0.0"
|
||||||
implementation 'androidx.preference:preference:1.1.1'
|
implementation 'androidx.preference:preference:1.1.1'
|
||||||
testImplementation 'junit:junit:4.13'
|
testImplementation 'junit:junit:4.13'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="de.hft.geotracker">
|
package="de.hft.geotracker">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||||
|
|
||||||
|
<!-- Required if your app targets Android 10 (API level 29) or higher -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package de.hft.geotracker
|
package de.hft.geotracker
|
||||||
|
|
||||||
|
import android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.location.Location
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.os.Looper
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuInflater
|
import android.view.MenuInflater
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
@ -11,20 +15,46 @@ import android.view.Window
|
|||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
import android.widget.AutoCompleteTextView
|
import android.widget.AutoCompleteTextView
|
||||||
import android.widget.Spinner
|
import android.widget.Spinner
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.core.app.ActivityCompat
|
||||||
|
import androidx.core.app.ActivityCompat.requestPermissions
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.findNavController
|
||||||
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.DataBindingUtil
|
||||||
import androidx.navigation.ui.NavigationUI
|
import androidx.navigation.ui.NavigationUI
|
||||||
|
import com.google.android.gms.location.*
|
||||||
import com.google.android.material.textfield.TextInputLayout
|
import com.google.android.material.textfield.TextInputLayout
|
||||||
|
import java.util.jar.Manifest
|
||||||
|
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
lateinit var fusedLocationClient : FusedLocationProviderClient
|
||||||
|
lateinit var locationRequest : LocationRequest
|
||||||
|
lateinit var locationCallback: LocationCallback
|
||||||
|
lateinit var actionButton : TextView
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
setSupportActionBar(findViewById(R.id.my_toolbar))
|
setSupportActionBar(findViewById(R.id.my_toolbar))
|
||||||
setContentView(R.layout.activity_home)
|
setContentView(R.layout.activity_home)
|
||||||
|
|
||||||
|
// val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
|
||||||
|
createLocationRequest()
|
||||||
|
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
||||||
|
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) !=
|
||||||
|
PackageManager.PERMISSION_GRANTED) {
|
||||||
|
requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), 1000)
|
||||||
|
}
|
||||||
|
locationCallback = object : LocationCallback() {
|
||||||
|
override fun onLocationResult(locationResult: LocationResult?) {
|
||||||
|
locationResult ?: return
|
||||||
|
for (location in locationResult.locations){
|
||||||
|
// Update UI with location data
|
||||||
|
findViewById<TextView>(R.id.latitude).text = location.latitude.toString()
|
||||||
|
findViewById<TextView>(R.id.longitude).text = location.longitude.toString()
|
||||||
|
findViewById<TextView>(R.id.altitude).text = location.altitude.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// val binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
|
// val binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
|
||||||
// val navController = this.findNavController(R.id.HostFragment)
|
// val navController = this.findNavController(R.id.HostFragment)
|
||||||
// NavigationUI.setupActionBarWithNavController(this, navController)
|
// NavigationUI.setupActionBarWithNavController(this, navController)
|
||||||
@ -49,8 +79,43 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// Apply the adapter to the spinner
|
// Apply the adapter to the spinner
|
||||||
spinner.adapter = adapter
|
spinner.adapter = adapter
|
||||||
}
|
}
|
||||||
|
actionButton = findViewById(R.id.button_start_stop)
|
||||||
|
actionButton.setOnClickListener {
|
||||||
|
callStartStop()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun callStartStop() {
|
||||||
|
println("StartStop pressed")
|
||||||
|
fusedLocationClient.lastLocation
|
||||||
|
.addOnSuccessListener { location : Location? ->
|
||||||
|
if (location != null) {
|
||||||
|
var label = findViewById<TextView>(R.id.display_acc)
|
||||||
|
label.text = location.latitude.toString()
|
||||||
|
|
||||||
|
} else {
|
||||||
|
println("Location = null")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
startLocationUpdates()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun startLocationUpdates() {
|
||||||
|
fusedLocationClient.requestLocationUpdates(locationRequest,
|
||||||
|
locationCallback,
|
||||||
|
Looper.getMainLooper())
|
||||||
|
}
|
||||||
|
fun createLocationRequest() {
|
||||||
|
locationRequest = LocationRequest.create().apply {
|
||||||
|
interval = 10000
|
||||||
|
fastestInterval = 5000
|
||||||
|
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
|
||||||
|
}
|
||||||
|
}
|
||||||
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
||||||
R.id.settings -> {
|
R.id.settings -> {
|
||||||
// User chose the "Settings" item, show the app settings UI...
|
// User chose the "Settings" item, show the app settings UI...
|
||||||
|
@ -83,9 +83,42 @@
|
|||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/my_toolbar" />
|
app:layout_constraintTop_toBottomOf="@+id/my_toolbar" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/altitude"
|
||||||
|
style="@style/text_style"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:text="dummy"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/longitude" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/latitude"
|
||||||
|
style="@style/text_style"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:text="dummy"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/display_acc" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/longitude"
|
||||||
|
style="@style/text_style"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:text="dummy"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/latitude" />
|
||||||
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user