firebase / geofire-java

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

onKeyEntered() method not getting call even data available #66

Closed jondoes closed 7 years ago

jondoes commented 7 years ago

When I pass longitude as positive value then it not calling onKeyEntered() method. It directly call onGeoQueryReady() method even data available in radius. When I pass longitude as negative value then it is calling onKeyEntered() method.

My code is as below,

private DatabaseReference firebaseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("my_database_url");

private GeoFire geoFire = new GeoFire(firebaseRef.child("geofire"));

GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(23.022505, 72.5713621), 1000);

        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {
                    Log.i(TAG, "onKeyEntered Key = " + key);
                    System.out.println(String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
                }
                @Override
                public void onKeyExited(String key) {
                    Log.i(TAG, "onKeyExited Key = " + key);
                    System.out.println(String.format("Key %s is no longer in the search area", key));
                }
                @Override
                public void onKeyMoved(String key, GeoLocation location) {
                    Log.i(TAG, "onKeyMoved Key = " + key);
                    System.out.println(String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude));
                }
                @Override
                public void onGeoQueryReady() {
                    Log.i(TAG, "onGeoQueryReady");
                    System.out.println("All initial data has been loaded and events have been fired!");
                }
                @Override
                public void onGeoQueryError(DatabaseError error) {
                    Log.i(TAG, "onGeoQueryError DatabaseError = " + error.getMessage());
                    System.err.println("There was an error with this query: " + error);
                }
            });

My firebase data screen shot attached below. screenshot from 2017-03-25 16-48-47

jwngr commented 7 years ago

Can you provide the latitude / longitude pair you are using which returns no results? You only have one key which has a negative latitude so unless you are close to that value, nothing will be returned. Also, how are you adding items to your GeoFire index? Can you share the code for that please?

jondoes commented 7 years ago

Thank you for your reply

I am using below latitude / longitude pair to fetch data. Latitude: 23.022505 Longitude: 72.5713621

Following code used to store data on firebase,

if let latitude = self.latitude, let longitude = self.longitude
                {
                        var jsonDict = Dictionary<String, AnyObject>()
                    jsonDict[Global.dbKeys.locKey] = Global.dbKeys.locValue as AnyObject?
                    jsonDict[Global.dbKeys.locDictKey] = [Global.dbKeys.locDictLat : latitude, Global.dbKeys.locDictLong : longitude] as AnyObject?
                    registerBusinessInAllNodesDict["\(FirebaseDataService.ds.REF_GEOFIRE.child(childRef.key))".trimFirebaseHost()] = jsonDict as AnyObject?
                }
jwngr commented 7 years ago

Are you actually calling geoFire.setLocation() at any point? That is how you are supposed to add data to your Firebase index. I don't see a call to that in the code you shared. That might be your problem.

jondoes commented 7 years ago

My mistake was, I have not use geoFire.setLocation() method. By using this my issue got resolve. Thank you for support.

muankit commented 5 years ago

hey @jondoes , i have the same problem . I did everything discussed here but still onKeyEntered method not called only onGeoQueryReady method called and update marker in realtime .

I have the following database structure.

fireloc

My firebase security rules are open (i.e. test mode) . And using "com.firebase:geofire-android:3.0.0" with other firebase dependencies : firebase-core:16.0.9 , firebase-auth:17.0.0 ,firebase-database:17.0.0 , firebase-ui-database:5.0.0 , firebase-storage:17.0.0

And here is my java code

    DatabaseReference locationRef = 
    FirebaseDatabase.getInstance().getReference().child("Location").child(mUID);

    GeoFire geoFire = new GeoFire(locationRef);

    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(mUser.latitude,mUser.longitude),1);
    geoQuery.removeAllListeners();

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {

            showMarker();
            showNotification("Entered");
            Log.d(TAG, "onKeyEntered: called ");
       }

        @Override
        public void onKeyExited(String key) {

            showMarker();
            Log.d(TAG, "onKeyExited: called");
            showNotification("Exited");

        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {

            Log.d(TAG, "onKeyMoved: called");

        }

        @Override
        public void onGeoQueryReady() {

            Log.d(TAG, "onGeoQueryReady: called");
            showMarker();
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    }); 

Q. What i want to do ? Ans: I want user's to see live location of other by updating marker on map. And i want to show notification when user enters and exits a specific location (i.e mUser.lattitude and mUser.longitude) .

I hope you understand my problem .

akin1108 commented 4 years ago

@muankit did you find a solution ? I'm also facing same issue. please help me out