firebase / geofire-java

GeoFire for Java - Realtime location queries with Firebase
MIT License
670 stars 271 forks source link

GeoFire.setCenter() not firing GeoQueryEventListener when set #14

Closed xsorifc28 closed 9 years ago

xsorifc28 commented 9 years ago

I expect the following code to fire the attached listener:

mapQuery.addGeoQueryEventListener(newMapQueryListener);
...
mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition position) {
            LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
            queryLocation = new GeoLocation(bounds.getCenter().latitude,bounds.getCenter().longitude);
            Log.i(TAG, "Map Camera Changed: " + queryLocation.latitude + ", " + queryLocation.longitude);
            mapQuery.setCenter(queryLocation);
            mapQuery.setRadius(defaultRadius);
            mClusterManager.onCameraChange(position);
        }
    });

but it never fires the listener..

Am I doing something wrong here ?

jwngr commented 9 years ago

The GeoQueryEventListener will fire when an item in your GeoFire index matches your query. Are you sure you actually have a point in your GeoFire index that is within the center and radius that you are setting?

xsorifc28 commented 9 years ago

Yes. I have two locations in the 'g' index. When the map loads, a marker is placed on the map. When I move the map, I expect that marker to go away and a different marker to show up at a different location once setCenter() is called.

xsorifc28 commented 9 years ago

Update: After debugging, it seems like GeoQuery.hasListeners() is returning false, even though I have already set a listener.

jwngr commented 9 years ago

Can you provide more code to help us debug this? What does newMapQueryListener look like? Can you add some debugging to make sure setCenter() is being called and that your listener is being added at all? Without a repro, it's going to be hard for us to track this down.

xsorifc28 commented 9 years ago

Below is the query listener. I add the user to a Google Map Cluster, and remove when they are out of the search area, keeping track of the user & key via a HashMap.

The listener is being added because I see the logcat outputs.

OnCameraChangeListener is called everytime I move the map (see first post), and then setCenter() is called within there, I/Map: Map Camera Changed: 29.9438741, -90.0720485 , but the query listener is never fired.

private GeoQueryEventListener newMapQueryListener = new GeoQueryEventListener() {
    @Override
    public void onKeyEntered(final String key, final GeoLocation location) {
        Log.i(TAG+"/onKeyEntered", key);

        userAction.query(User.getQueriableInstance(key), new ExistenceActionCallback<User>() {
            @Override
            public void onExist(User user) {
                user.setLocation(new LatLng(location.latitude, location.longitude));
                clusterUserMap.put(key, user);
                Log.i(TAG + "/onKeyEntered", "Add new user: " + user.getFirstName());
                mClusterManager.addItem(user);
            }

            @Override
            public void onError(FirebaseError firebaseError) {
                Log.i(TAG + "/onKeyEntered", "Error getting user: " + firebaseError.getMessage());
            }
        });

    }

    @Override
    public void onKeyExited(String key) {
        Log.i(TAG +"/onKeyExited", key);
        mClusterManager.removeItem(clusterUserMap.get(key));
        clusterUserMap.remove(key);

    }

    @Override
    public void onKeyMoved(String key, GeoLocation location) {
        Log.i(TAG +"/onKeyMoved", key);
        if(clusterUserMap.containsKey(key)) {
            Log.i(TAG, "Old Location: " + clusterUserMap.get(key).getLocation());
            Log.i(TAG, "New Location: " + location);
            mClusterManager.removeItem(clusterUserMap.get(key));
            clusterUserMap.get(key).setLocation(new LatLng(location.latitude, location.longitude));
            mClusterManager.addItem(clusterUserMap.get(key));
        }
    }

    @Override
    public void onGeoQueryReady() {
        mClusterManager.cluster();
    }

    @Override
    public void onGeoQueryError(FirebaseError error) {

    }
};
xsorifc28 commented 9 years ago

Update:

I had this in onCreate() :

if(loc != null) {
        queryLocation = new GeoLocation(loc.getLatitude(), loc.getLongitude());

        mapQuery = locAction.queryAtLocation(loc, DEFAULT_KILOMETERS);

        mapQuery.addGeoQueryEventListener(newMapQueryListener);

    } else {
        locAction.enableGPSDialog().show();

and this on onResume() :

    if(loc != null) {
        queryLocation = new GeoLocation(loc.getLatitude(), loc.getLongitude());
        mapQuery = locAction.queryAtLocation(loc, DEFAULT_KILOMETERS);
    } else {
        locAction.enableGPSDialog().show();
    }

Seems like setting the mapQuery again was removing the listener. Didn't see this before, as Android Studio / intelliJ was not showing me all usages of the mapQuery variable!

The issue is now fixed. Thank you for your help!

jwngr commented 9 years ago

Great, glad you got it all fixed! Closing this issue.

champrocks3190 commented 4 years ago

Hello how did you solve can you please explain ? i am facing the same issue ,

i want to remove old markers of users location on google map but the Geoquery.setCenter or Geoquery.setLocation both don't work when user A is moving

if user A Is the app user tracking nearby users if B user is moving out of radius or in to radius A gets update on google map when A is static

but if B is not moving and A IS Moving than the B don't get automatically exited i means at that time onKeyExited don't notify that the A has new center location.

any solution for that ?

champrocks3190 commented 4 years ago

Update:

I had this in onCreate() :

if(loc != null) {
        queryLocation = new GeoLocation(loc.getLatitude(), loc.getLongitude());

        mapQuery = locAction.queryAtLocation(loc, DEFAULT_KILOMETERS);

        mapQuery.addGeoQueryEventListener(newMapQueryListener);

    } else {
        locAction.enableGPSDialog().show();

and this on onResume() :

    if(loc != null) {
        queryLocation = new GeoLocation(loc.getLatitude(), loc.getLongitude());
        mapQuery = locAction.queryAtLocation(loc, DEFAULT_KILOMETERS);
    } else {
        locAction.enableGPSDialog().show();
    }

Seems like setting the mapQuery again was removing the listener. Didn't see this before, as Android Studio / intelliJ was not showing me all usages of the mapQuery variable!

The issue is now fixed. Thank you for your help!

Hello how did you solve can you please explain ? i am facing the same issue ,

i want to remove old markers of users location on google map but the Geoquery.setCenter or Geoquery.setLocation both don't work when user A is moving

if user A Is the app user tracking nearby users if B user is moving out of radius or in to radius A gets update on google map when A is static

but if B is not moving and A IS Moving than the B don't get automatically exited i means at that time onKeyExited don't notify that the A has new center location.

any solution for that ?