Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Native Mobile Apps 2020 Fall
Course Materials
Commits
44e707cc
Commit
44e707cc
authored
Oct 09, 2020
by
Andres Käver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
location
parent
e92cd19c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
5 deletions
+52
-5
android/GPSMapDemo/app/src/main/java/ee/taltech/mobile2020/gpsmapdemo/MapsActivity.kt
...ain/java/ee/taltech/mobile2020/gpsmapdemo/MapsActivity.kt
+52
-5
No files found.
android/GPSMapDemo/app/src/main/java/ee/taltech/mobile2020/gpsmapdemo/MapsActivity.kt
View file @
44e707cc
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment