简单的问题需要简单的答案。如何跟踪特定地理围栏中的停留时间。当我使用 Trigger on Enter 添加地理围栏时,我会在离开该地理围栏时自动收到一个触发器。基本上我需要记住我进入地理围栏和离开地理围栏的时间,这样我就可以减去离开进入时间并获得我的持续时间。但我相信做到这一点并不容易。那么任何其他想法或建议如何有效地解决该问题?谢谢
我为这个问题找到了一个解决方案。为了确定地理围栏的停留时间,我必须获得用户触发地理围栏的时间,然后我使用这种数学方法来计算用户是否离开地理围栏
Handler handler = new Handler();
getCurrentLatLng();
final Runnable myRunnable = new Runnable() {
public void run() {
GeofenceFinished myFinishedGeofence = db.getGeofenceFinishedByID("Finished" + id);
if (!isUserInRegion(latitude, longitude, myLatLng.latitude, myLatLng.longitude, rd)
|| myFinishedGeofence == null) {
String time = convertTime(endTime - startTime);
// get the finishedGeofence and set the duration of stay time
if (myFinishedGeofence != null)
setDurationOfStay("Finished" + id, time, getCurrentTime(System.currentTimeMillis()));
Toast.makeText(mContext, "Finish to determine duration of stay of " + myFinishedGeofence.getAddress(), Toast.LENGTH_SHORT).show();
handler.removeCallbacks(this);
} else {
getCurrentLatLng();
endTime += Constants.DELAY;
handler.postDelayed(this, Constants.DELAY);
}
}
private void setDurationOfStay(String geofenceid, String time, String endTime) {
if (db == null) db = GeofenceDatabaseHelper.getInstance(mContext);
if (!db.setDurationOfFinishedGeofence(geofenceid, time, endTime)) {
Log.i("setDurationOfStay", "fail");
}
}
};
handler.postDelayed(myRunnable, Constants.DELAY);
private boolean isUserInRegion(double firstLat, double firstLog, double curLat, double curLog, float radius) {
double distance = calculateDistance(firstLat, firstLog, curLat, curLog);
return distance <= radius;
}
为了计算用户和地理围栏中心的距离,我需要以米为单位转换当前的纬度和经度,这样我就可以用圆的参数方程来计算
所以我使用github的这种方法
// https://github.com/mgavaghan/geodesy
private double calculateDistance(double firstLat, double firstLog, double curLat, double curLog) {
GeodeticCalculator geoCalc = new GeodeticCalculator();
Ellipsoid reference = Ellipsoid.WGS84;
GlobalPosition pointA = new GlobalPosition(firstLat, firstLog, 0.0); // Point A
GlobalPosition userPos = new GlobalPosition(curLat, curLog, 0.0); // Point B
// Distance between Point A and Point B
double distance = geoCalc.calculateGeodeticCurve(reference, userPos, pointA).getEllipsoidalDistance();
return distance;
}
希望它可以帮助某人:D
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句