我正在尝试在Android应用中使用GeoFire实施地理围栏。
它正在工作,但是有问题。当用户的位置在地理围栏区域内时,将执行onKeyMoved方法,但不会启动其他两个方法onKeyEntered和onKeyExited。
这是我当前的代码:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng zona = new LatLng(41.635189,2.163086);
mMap.addCircle(new CircleOptions()
.center(zona)
.radius(500)
.strokeColor(Color.BLUE)
.fillColor(0x220000FF)
.strokeWidth(5.0f)
);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(zona.latitude,zona.longitude),05.f);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Log.d("MPI","ENTRA en la zona ");
sendNotification("MPI",String.format("%s entra en la zona",key));
}
@Override
public void onKeyExited(String key) {
Log.d("MPI","SALE en la zona ");
sendNotification("MPI",String.format("%s sale de la zona",key));
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
Log.d("MPI","esta en la zona ");
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
private void sendNotification(String title, String content) {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(content);
NotificationManager manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MapsActivity.class);
PendingIntent contenIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(contenIntent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |=Notification.DEFAULT_SOUND;
manager.notify(new Random().nextInt(),notification);
}
也许您可以找到问题所在。