我在我的 ionic 应用程序 android 版本中面临有关地理围栏的问题。多次调用半径输出/输入条目。当一个人可以进入半径或从半径出去时,它应该正确调用一次。
我们也在使用事件总线。
在这里,我们在 android 文件中有代码
调用地理围栏
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
Intent i = new Intent("geofenceStatus");
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
status = "exit";
} else {
if (status.equals("exit")) status = "reenter";
else status = "enter";
}
Date currentTime = Calendar.getInstance().getTime();
i.putExtra("status", status);
LocalBroadcastManager.getInstance(this).sendBroadcast(i);
EventBus.getDefault().post(status);
// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getTransitionString(geofenceTransition);//getGeofenceTransitionDetails(geofenceTransition, triggeringGeofences);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
} else {
// Log the error.
}
添加地理围栏
mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
.addOnFailureListener(e -> {
Log.e(TAG, "onFailure: " + e.toString());
//Toast.makeText(activity, "Failed to add geoFence, " + e.toString(), Toast.LENGTH_SHORT).show();
})
.addOnCompleteListener(task -> {
Log.d(TAG, "onComplete: GeoFence added status: " + task.isSuccessful());
if (task.isSuccessful()) {
Calendar c = Calendar.getInstance();
geofenceCreateDate = String.valueOf(c.get(Calendar.DATE));
Toast.makeText(activity, "GeoFence Added", Toast.LENGTH_SHORT).show();
PrefUtils.saveToPrefs(activity, "geofenceCreateDate", geofenceCreateDate);
}
});
删除地理围栏
mGeofencingClient.removeGeofences(getGeofencePendingIntent()).addOnCompleteListener(task -> {
Log.d(TAG, "removeGeofences: GeoFence remove status: " + task.isSuccessful());
PrefUtils.removeFromPrefs(activity, "GEO_STATUS");
//if (task.isSuccessful())
// Toast.makeText(activity, "GeoFence Removed", Toast.LENGTH_SHORT).show();
});
客户端调用请求如下
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "onFailure: api failed" + e.getLocalizedMessage());
call.cancel();
}
@Override
public void onResponse(Call call, Response response) {
try {
//response.body() can be consumed only once so use only jsonData
String jsonData = response.body().string();
Log.d(TAG, "onResponse: " + jsonData);
JSONObject jsonObj = new JSONObject(jsonData);
String status = jsonObj.getString("status");
if (status != null && status.equals("success")) {
JSONArray jsonRes = jsonObj.getJSONArray("result");
JSONObject obj = jsonRes.getJSONObject(0);
latitude = Double.parseDouble(obj.getString("latitude"));
longitude = Double.parseDouble(obj.getString("longitude"));
//GEOFENCE_RADIUS_IN_METERS = Integer.parseInt(obj.getString("geofencing_metre"));
removeGeofence();
addGeoFence();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
这是我们打印的一些日志,显示了多个调用
2021-04-23 09:45:58 - {"id":0,"user_id":"127","job_id":"945","task_code_id":0,"action":"out","time_entry_id":0,"latitude":"42.3430848","longitude":"-85.2745107","app_version":20200}
2021-04-23 09:45:58 - {"id":0,"user_id":"127","job_id":"847","task_code_id":0,"action":"out","time_entry_id":0,"latitude":"42.2929321","longitude":"-85.5819675","app_version":20200}
2021-04-23 09:45:58 - {"id":0,"user_id":"127","job_id":"775","task_code_id":0,"action":"out","time_entry_id":0,"latitude":"42.3501563","longitude":"-86.0732105","app_version":20200}
2021-04-23 09:45:58 - {"id":0}
2021-04-23 09:46:01 - {"id":0,"user_id":"127","job_id":"847","task_code_id":0,"action":"in","time_entry_id":0,"latitude":"42.2929321","longitude":"-85.5819675","app_version":20200}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0,"user_id":"127","job_id":"945","task_code_id":0,"action":"in","time_entry_id":0,"latitude":"42.3430848","longitude":"-85.2745107","app_version":20200}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0}
2021-04-23 09:46:01 - {"id":0,"user_id":"127","job_id":"775","task_code_id":0,"action":"in","time_entry_id":0,"latitude":"42.3501563","longitude":"-86.0732105","app_version":20200}