Commit 96f8f327 authored by wanglei's avatar wanglei

...

parent 2142265b
......@@ -7,6 +7,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.activity.addCallback
import androidx.core.location.LocationManagerCompat.requestLocationUpdates
import androidx.lifecycle.lifecycleScope
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.R
......@@ -229,7 +230,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
}
override fun onMapReady(googleMap: GoogleMap) {
LogEx.logDebug(TAG,"onMapReady")
LogEx.logDebug(TAG, "onMapReady")
map = googleMap
LogEx.logDebug(TAG, "onMapReady")
......@@ -368,6 +369,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
override fun onPause() {
super.onPause()
locationPresenter.cancelLocationUpdates()
locationPresenter.cancelUploadJob()
}
......@@ -382,7 +384,9 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
}
if (checkLocationPermission()) {
locationPresenter.requestLocationUpdates()
locationPresenter.startUploadMyLocation()
}
}
......
......@@ -3,12 +3,16 @@ package com.base.locationsharewhite.ui.locationmap
import android.annotation.SuppressLint
import android.app.Activity
import android.location.Location
import android.os.Looper
import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.location.LocationPositionUtils.uploadMyLocation
import com.base.locationsharewhite.map.MapUtils.getLatLngByLocationManager
import com.base.locationsharewhite.utils.BatteryUtils.getBatteryLevel
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
......@@ -86,4 +90,46 @@ class LocationPresenter(
uploadJob = null
}
private 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)
}
}
}
\ No newline at end of file
......@@ -50,7 +50,7 @@ class MainPresenter(
}
var locationCallback: LocationCallback? = null
private var locationCallback: LocationCallback? = null
/**
* 请求google service地理位置跟新
......
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