地理围栏列表未附加

时间:2019-11-13 08:38:35

标签: java android google-maps google-api

我正在实施地理围栏,并且我的地理围栏位置存储在Firebase中。 基本上,我的目标是检索所有存储的地理围栏并将它们放入geofenceList中,这是私有列表geofenceList = new ArrayList <>();

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(geofenceList);
    return builder.build();
}

此代码给我一个错误,因为“ java.lang.IllegalArgumentException:未向此请求添加地理围栏” ,并且看来我的geofenceList为空。但是,当执行以下操作时,我确保添加了geofenceList:

  1. 从Firebase(在OnCreate内部)检索地理围栏

    final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
    
    databaseReference.child(MyDatabase.USERS).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mGeoFenceList = new MyDatabase(ListActivity.this, databaseReference).readSnapshot(dataSnapshot);
            adapter.updateData(mGeoFenceList);
            for (int i = 0; i < mGeoFenceList.size(); i++) {
                MyLocation location = mGeoFenceList.get(i);
                buildGeofence(location);
            }
        }
    
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {}
    });
    
  2. 然后转到buildGeofence(location)

    public void buildGeofence(MyLocation entry) {
    geofenceList.add(new Geofence.Builder()
            .setRequestId(entry.getName())
            .setCircularRegion(
                    entry.getLat(),
                    entry.getLng(),
                    (float) entry.getRadius()
            )
            .setExpirationDuration(5000)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT).build());
    

    }

  3. 此过程完成后,我调用geofenceClient.addGeofences(),

    geofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent()) {...}
    
  4. 但这给我一个错误,因为每当我调用getGeofencingRequest()时:

    private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(geofenceList);
        return builder.build();}
    

任何帮助将不胜感激! 非常感谢您拯救我的生命:')

0 个答案:

没有答案