如何删除所有加入Google Geofence API的地理围栏?

时间:2016-05-18 08:53:53

标签: google-maps google-maps-api-3 google-api-client android-geofence

我在that example中添加了地理围栏到Google Geofence API。我不时会删除那些地理围栏(逐个或全部),与其到期时间等无关。

mGeofenceList.add(new Geofence.Builder()
    // Set the request ID of the geofence. This is a string to identify this
    // geofence.
    .setRequestId(entry.getKey())

    .setCircularRegion(
            entry.getValue().latitude,
            entry.getValue().longitude,
            Constants.GEOFENCE_RADIUS_IN_METERS
    )
    .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
            Geofence.GEOFENCE_TRANSITION_EXIT)
    .build());

如何删除自己输入Google Geofence API的地理围栏?

2 个答案:

答案 0 :(得分:2)

调用removeGeofences(GoogleApiClient client, PedingIntent pendingIntent)以删除与给定pendingIntent关联的所有地理围栏。

在创建待处理意图时使用FLAG_UPDATE_CURRENT而不是FLAG_CANCEL_CURRENT,否则您将无法获得为addGeofences(GoogleApiClient,List,PendingIntent)https://developers.google.com/android/reference/com/google/android/gms/location/GeofencingApi.html#addGeofences(com.google.android.gms.common.api.GoogleApiClient,com提供的相同待处理意图。 google.android.gms.location.GeofencingRequest,android.app.PendingIntent)]因此删除操作将不会删除任何内容。

答案 1 :(得分:0)

Syntax: geofencingClient.removeGeofences(getPendingIntent());

Note : geofencingClient is an Object to which geofences were added and 
getPendingIntent() is function which you used earlier.

getPendingIntent()函数如下:

public PendingIntent getPendingIntent() {
        if (pendingIntent != null) {
            return pendingIntent;
        }
        Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 2607, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        return pendingIntent;
    }
// GeofenceBroadcastReceiver.class (Change this wrt to your code)

要删除地理围栏时,请调用此函数:

void remove(){
        geofencingClient.removeGeofences(getPendingIntent());
    }