转换后删除触发的地理围栏

remove triggered geofence after transition

我试图在输入地理围栏后将其删除,以阻止它重新触发输入转换。从一个类似的问题中,我得到了这条行之有效的行

LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient,getGeofencePendingIntent()).setResultCallback(this);

但是它会删除所有地理围栏,而不仅仅是已触发的地理围栏。

我需要做什么才能select删除正确的 ID?

您需要传递用于构建地理围栏的相同字符串。

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);

或者您可以从收到的 Intent 中获取地理围栏。

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);