Commit 44e707cc authored by Andres Käver's avatar Andres Käver

location

parent e92cd19c
......@@ -5,6 +5,7 @@ import android.content.Context
import android.content.pm.PackageManager
import android.location.Criteria
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
......@@ -18,7 +19,7 @@ import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, LocationListener {
private lateinit var mMap: GoogleMap
......@@ -29,15 +30,14 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// 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)
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
var criteria = Criteria()
provider = locationManager.getBestProvider(criteria, true)
Log.d("Provider", provider);
if (provider != null){
if (
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
......@@ -49,6 +49,12 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
}
// 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)
}
/**
......@@ -69,7 +75,48 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
markerLatLng = LatLng(location!!.latitude, location!!.longitude)
}
mMap.addMarker(MarkerOptions().position(markerLatLng).title("Marker"))
mMap.moveCamera(CameraUpdateFactory.zoomTo(15f));
mMap.moveCamera(CameraUpdateFactory.newLatLng(markerLatLng))
}
override fun onResume() {
super.onResume()
if (locationManager != null && provider != null){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return
}
locationManager.requestLocationUpdates(provider, 400, 1f, this)
}
}
override fun onPause() {
super.onPause()
if (locationManager != null && provider != null){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return
}
locationManager.removeUpdates( this)
}
}
override fun onLocationChanged(location: Location?) {
if (mMap == null) return
var markerLatLng = LatLng(location!!.latitude, location!!.longitude)
mMap.moveCamera(CameraUpdateFactory.newLatLng(markerLatLng))
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
TODO("Not yet implemented")
}
override fun onProviderEnabled(provider: String?) {
TODO("Not yet implemented")
}
override fun onProviderDisabled(provider: String?) {
TODO("Not yet implemented")
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment