【发布时间】:2021-09-29 16:39:51
【问题描述】:
我正在开发一个简单的应用程序,该应用程序可以在用户进入/移动/退出该区域时创建地理围栏和 detexts。 当用户输入特定的地理围栏时,我正在尝试删除地理围栏,但我找不到任何有关如何实施此操作的文档。
我正在创建两个地理围栏。
在模拟器上测试
private void addGeofence(LatLng latLng, float radius, String GEO_ID) {
Geofence geofence = geofenceHelper.getGeofence(GEO_ID, latLng, radius, Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL | Geofence.GEOFENCE_TRANSITION_EXIT);
GeofencingRequest geofencingRequest = geofenceHelper.geofencingRequest(geofence);
PendingIntent pendingIntent = geofenceHelper.getPendingIntent();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
geofencingClient.addGeofences(geofencingRequest, pendingIntent).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.e("TAG", "Geocenfe added");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String errorMessage = geofenceHelper.getErrorString(e);
Log.e("TAG", "onFailure: " + errorMessage);
Toast.makeText(getApplicationContext(), "onFailure: " + errorMessage, Toast.LENGTH_SHORT).show();
}
});
}
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
Log.e("TAG", "onReceive: Error geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence: geofenceList) {
Log.e("TAG", "ID: " + geofence.getRequestId());
}
int transitionType = geofencingEvent.getGeofenceTransition();
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Log.e("TAG", "GEOFENCE_TRANSITION_ENTER: ");
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
// Log.e("TAG", "GEOFENCE_TRANSITION_DWELL: ");
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Log.e("TAG", "GEOFENCE_TRANSITION_EXIT: ");
break;
}
}
}
【问题讨论】:
标签: java android gps geofencing android-geofence