我尝试在输入地理围栏后删除地理围栏,以阻止它重新触发输入转换。从一个类似的问题,我得到了这一行很好的
{{1}}
然而,它会删除所有地理围栏,而不仅仅是已触发的地理围栏。
为了选择要删除的ID,我需要做什么?
答案 0 :(得分:6)
您需要传递用于构建Geofence的相同String。
String geofenceId = "randomId";
Geofence geofence = new Geofence.Builder()
.setRequestId(geofenceId)
....
.build();
GeofencingRequest request = new GeofencingRequest.Builder()
.addGeofence(geofence)
....
.build();
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, request, pendingIntent);
要删除地理围栏,可以使用
List<String> geofencesToRemove = new ArrayList<>();
geofencesToRemove.add(geofenceId);
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, geofencesToRemove);
或者你可以从你收到的意图中获得地理围栏。
GeofencingEvent event = GeofencingEvent.fromIntent( intent_you_got_from_geofence );
List<Geofence> triggeredGeofences = event.getTriggeringGeofences();
List<String> toRemove = new ArrayList<>();
for (Geofence geofence : triggeredGeofences) {
toRemove.add(geofence.getRequestId());
}
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, toRemove);