英文:
Limit maximum GPS between 2 distances
问题
以下是翻译好的内容:
我已经阅读了许多类似的文章,但没有得到我需要的答案:
因此,我有一个连接到后端服务器的应用程序,类似于现场考勤,在这种情况下,用户(在这种情况下是技术人员)前往特定的地点,并使用照片功能进行文档记录,其中具有自动水印功能(显示坐标和到站点的距离)。
我设置了一个坐标,用户必须访问并拍照,但我想限制到站点的距离,因此如果用户拍摄的照片距离站点的容差超过100米,应用程序会自动关闭。
目前我在开发中同时使用Java和Kotlin,
我如何实现这一点?
这是获取位置的代码:
class LocationService(context: Context): LiveData<Result<Throwable, Location?>>() {
private var fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
override fun onInactive() { // 当生命周期所有者(LocationActivity)暂停、停止或销毁时调用
super.onInactive()
fusedLocationClient.removeLocationUpdates(locationCallback)
}
override fun onActive() { // 当生命周期所有者(LocationActivity)启动或恢复时调用
super.onActive()
fusedLocationClient.lastLocation
.addOnSuccessListener { setLocationData(it) }
.addOnFailureListener {t -> setErrorData(t) }
startLocationUpdates()
}
private fun setLocationData(location: Location?) { // location可为空,因为即使上次已检索到最后一个位置,但如果关闭了GPS,禁用位置会清除缓存
//value = LocationEvent.OnLocationResult(Result.build { location })
value = Result.build { location }
}
private fun setErrorData(t: Throwable) {
//value = LocationEvent.OnLocationResult(Result.build { throw t })
value = Result.build { throw t }
}
private fun startLocationUpdates() {
Timber.d { "开始位置更新" }
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
null // looper
)
}
}
英文:
I have read many articles similar to this, but I didn't get answers I need:
So, I have an application connected to my back-end server, like onSite Attendance where users (technicians in this case) go to certain places and do documentations there using photo features where it has automatic watermark feature (showing coordinates and distance to site),
I set a coordinate where users have to visit and take pictures, but I want to limit distance to site, so if users take pictures more than 100 metres tolerance to site, the app automatically close,
Currently I am using both Java and Kotlin for development,
How can I achieve this?
This is the code for getting location:
class LocationService(context: Context): LiveData<Result<Throwable, Location?>>() {
private var fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
override fun onInactive() { // is called when the lifecycle owner(LocationActivity) is either paused, stopped or destroyed
super.onInactive()
fusedLocationClient.removeLocationUpdates(locationCallback)
}
override fun onActive() { // is called when the lifecycle owner(LocationActivity) is either started or resumed
super.onActive()
fusedLocationClient.lastLocation
.addOnSuccessListener { setLocationData(it) }
.addOnFailureListener {t -> setErrorData(t) }
startLocationUpdates()
}
private fun setLocationData(location: Location?) { // location is nullable as if GPS is turned off even if the last
// location was previously retrieve because disabling location clears the cache
//value = LocationEvent.OnLocationResult(Result.build { location })
value = Result.build { location }
}
private fun setErrorData(t: Throwable) {
//value = LocationEvent.OnLocationResult(Result.build { throw t })
value = Result.build { throw t }
}
private fun startLocationUpdates() {
Timber.d { "start location updates" }
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
null // looper
)
}
答案1
得分: 2
你可以使用Android的地理围栏。
地理围栏将用户当前位置的意识与用户与可能感兴趣的位置的接近意识相结合。要标记感兴趣的位置,您需要指定其纬度和经度。要调整位置的接近度,您需要添加一个半径。纬度、经度和半径定义了一个地理围栏,创建了一个围绕感兴趣位置的圆形区域,或称之为围栏。
您可以拥有多个活动地理围栏,每个应用程序每个设备用户限制为100个。对于每个地理围栏,您可以要求位置服务向您发送进入和退出事件,或者您可以指定在地理围栏区域内等待或停留的持续时间,然后触发事件。您可以通过指定以毫秒为单位的过期持续时间来限制任何地理围栏的持续时间。地理围栏过期后,位置服务会自动移除它。 更多信息
英文:
You can use Android Geofencing
Geofencing combines awareness of the user's current location with awareness of the user's proximity to locations that may be of interest. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence, creating a circular area, or fence, around the location of interest.
You can have multiple active geofences, with a limit of 100 per app, per device user. For each geofence, you can ask Location Services to send you entrance and exit events, or you can specify a duration within the geofence area to wait, or dwell, before triggering an event. You can limit the duration of any geofence by specifying an expiration duration in milliseconds. After the geofence expires, Location Services automatically removes it. more
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论