Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
L
location share white
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
location share white
Commits
2142265b
Commit
2142265b
authored
Oct 31, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
ba3f81d7
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
19 deletions
+132
-19
MainActivity.kt
.../java/com/base/locationsharewhite/ui/main/MainActivity.kt
+38
-15
MainPresenter.kt
...java/com/base/locationsharewhite/ui/main/MainPresenter.kt
+94
-4
No files found.
app/src/main/java/com/base/locationsharewhite/ui/main/MainActivity.kt
View file @
2142265b
...
...
@@ -7,6 +7,7 @@ import android.os.Build
import
android.view.LayoutInflater
import
androidx.activity.addCallback
import
androidx.core.view.updatePadding
import
androidx.lifecycle.lifecycleScope
import
com.base.locationsharewhite.R
import
com.base.locationsharewhite.databinding.ActivityMainBinding
import
com.base.locationsharewhite.fcm.PopupConstObject
...
...
@@ -17,7 +18,6 @@ import com.base.locationsharewhite.helper.BaseActivity
import
com.base.locationsharewhite.helper.MyApplication
import
com.base.locationsharewhite.location.LocationLoginUtils
import
com.base.locationsharewhite.map.MapUtils.addLocationMarker
import
com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import
com.base.locationsharewhite.ui.howuse.HowUseActivity
import
com.base.locationsharewhite.ui.locationmap.LocationMapActivity
import
com.base.locationsharewhite.ui.set.SettingActivity
...
...
@@ -143,29 +143,52 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {
if
(
isRecreate
)
{
return
}
mainPresenter
.
requestLocationUpdates
()
mainPresenter
.
startLocationJob
(
lifecycleScope
)
LogEx
.
logDebug
(
TAG
,
"onResume isRecreate=$isRecreate"
)
}
override
fun
onPause
()
{
super
.
onPause
()
mainPresenter
.
cancelLocationUpdates
()
mainPresenter
.
cancelLocationJob
()
}
var
reSetZoom
=
true
override
fun
onMapReady
(
googleMap
:
GoogleMap
)
{
map
=
googleMap
val
latLng
:
LatLng
?
=
getLatLngByLocationManager
(
)
if
(
latLng
==
null
)
{
mainPresenter
.
getGoogleServiceLocation
()
mainPresenter
.
locationCallBack
=
{
if
(
marker
==
null
)
{
changeLocation
(
it
)
LogEx
.
logDebug
(
TAG
,
"onMapReady"
)
mainPresenter
.
startLocationJob
(
lifecycleScope
)
mainPresenter
.
uiLocationCallback
=
{
LogEx
.
logDebug
(
TAG
,
"uiLocationCallback $it"
)
changeLocation
(
it
,
true
,
reSetZoom
)
reSetZoom
=
false
}
}
private
fun
changeLocation
(
latLng
:
LatLng
,
cameraFollow
:
Boolean
,
reSetZoom
:
Boolean
)
{
LogEx
.
logDebug
(
TAG
,
"getLastKnowLocation ${latLng.latitude} ${latLng.longitude}"
)
initMyMarker
(
latLng
)
if
(
cameraFollow
)
{
if
(
reSetZoom
)
{
map
?.
moveCamera
(
CameraUpdateFactory
.
newLatLngZoom
(
latLng
,
15f
))
}
else
{
changeLocation
(
latLng
)
map
?.
moveCamera
(
CameraUpdateFactory
.
newLatLng
(
latLng
))
}
}
}
private
fun
changeLocation
(
latLng
:
LatLng
)
{
LogEx
.
logDebug
(
TAG
,
"getLastKnowLocation ${latLng.latitude} ${latLng.longitude}"
)
/**
* 初始化marker
*/
private
fun
initMyMarker
(
latLng
:
LatLng
)
{
if
(
marker
==
null
)
{
val
myAvatar
=
LayoutInflater
.
from
(
this
).
inflate
(
R
.
layout
.
avatar_me
,
null
)
map
?.
moveCamera
(
CameraUpdateFactory
.
newLatLngZoom
(
latLng
,
15f
))
marker
=
map
?.
addLocationMarker
(
myAvatar
,
latLng
,
"my"
)
}
}
}
\ No newline at end of file
app/src/main/java/com/base/locationsharewhite/ui/main/MainPresenter.kt
View file @
2142265b
...
...
@@ -3,9 +3,21 @@ package com.base.locationsharewhite.ui.main
import
android.annotation.SuppressLint
import
android.app.Activity
import
android.location.Location
import
android.os.Looper
import
androidx.lifecycle.LifecycleCoroutineScope
import
com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import
com.google.android.gms.location.FusedLocationProviderClient
import
com.google.android.gms.location.LocationCallback
import
com.google.android.gms.location.LocationRequest
import
com.google.android.gms.location.LocationResult
import
com.google.android.gms.location.LocationServices
import
com.google.android.gms.maps.model.LatLng
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.delay
import
kotlinx.coroutines.isActive
import
kotlinx.coroutines.launch
class
MainPresenter
(
val
activity
:
Activity
...
...
@@ -13,18 +25,96 @@ class MainPresenter(
//google的地理位置服务
private
var
fusedLocationClient
:
FusedLocationProviderClient
=
LocationServices
.
getFusedLocationProviderClient
(
activity
)
var
googleLocation
:
LatLng
?
=
null
private
var
googleLocation
:
LatLng
?
=
null
var
locationCallBack
:
((
latLng
:
LatLng
)
->
Unit
)?
=
null
@SuppressLint
(
"MissingPermission"
)
fun
getGoogleServiceLocation
()
{
private
fun
getGoogleServiceLocation
()
{
fusedLocationClient
.
lastLocation
.
addOnSuccessListener
{
location
:
Location
?
->
if
(
location
!=
null
)
{
val
latLng
=
LatLng
(
location
.
latitude
,
location
.
longitude
)
googleLocation
=
latLng
locationCallBack
?.
invoke
(
latLng
)
}
}
}
/**
* 获取自身位置,
*/
private
fun
getMyLocation
():
LatLng
?
{
var
latLng
=
activity
.
getLatLngByLocationManager
()
if
(
latLng
==
null
)
{
latLng
=
googleLocation
}
return
latLng
}
var
locationCallback
:
LocationCallback
?
=
null
/**
* 请求google service地理位置跟新
*/
@SuppressLint
(
"MissingPermission"
)
fun
requestLocationUpdates
()
{
// 创建LocationRequest对象,并设置参数
val
locationRequest
:
LocationRequest
=
LocationRequest
.
create
()
locationRequest
.
setInterval
(
10000
)
// 设置更新间隔为10秒
locationRequest
.
setFastestInterval
(
5000
)
// 设置最快更新间隔为5秒
locationRequest
.
setPriority
(
LocationRequest
.
PRIORITY_HIGH_ACCURACY
)
// 设置位置请求的优先级
locationCallback
=
object
:
LocationCallback
()
{
override
fun
onLocationResult
(
locationResult
:
LocationResult
)
{
for
(
location
in
locationResult
.
locations
)
{
// 在这里获取最新的经纬度
val
latitude
=
location
.
latitude
val
longitude
=
location
.
longitude
}
}
}
locationCallback
?.
let
{
// 请求位置更新
fusedLocationClient
.
requestLocationUpdates
(
locationRequest
,
it
,
Looper
.
getMainLooper
())
}
}
/**
* 取消google service地理位置更新
*/
fun
cancelLocationUpdates
()
{
locationCallback
?.
let
{
fusedLocationClient
.
removeLocationUpdates
(
it
)
}
}
var
uiLocationCallback
:
((
latLng
:
LatLng
)
->
Unit
)?
=
null
private
var
locationJob
:
Job
?
=
null
fun
startLocationJob
(
lifecycleCoroutineScope
:
LifecycleCoroutineScope
)
{
if
(
locationJob
==
null
)
{
locationJob
=
lifecycleCoroutineScope
.
launch
(
Dispatchers
.
IO
)
{
while
(
isActive
)
{
getGoogleServiceLocation
()
val
latLng
=
getMyLocation
()
launch
(
Dispatchers
.
Main
)
{
latLng
?.
let
{
uiLocationCallback
?.
invoke
(
it
)
}
}
delay
(
10
*
1000L
)
}
}
}
}
fun
cancelLocationJob
()
{
locationJob
?.
cancel
()
locationJob
=
null
}
}
\ 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