I have inserted location using geofire in firebase

三世轮回 提交于 2019-12-04 06:19:47

问题


I have inserted location using geofire in firebase from one of the activity in android studio now I want to retrieve all the location in single google map activity with marker. I have created map activity, but I don't know how to retrieve the location from the firebase .

Please help I am stuck in this. With some source code.

Here is my code for sending location to firebase

GeoFire geoFire = new 
GeoFire(databaseReference.child("Location").child(user.getUid()));
          geoFire.setLocation("location", new 
GeoLocation(location.getLatitude(), location.getLongitude()), new 
GeoFire.CompletionListener() {
              @Override
              public void onComplete(String key, DatabaseError error) {
                  if (error != null) {
                      Toast.makeText(gpdfuel.this, "There was an error 
saving the location to GeoFire: " + error, Toast.LENGTH_LONG).show();
                  } else {
                      Toast.makeText(gpdfuel.this, "Location saved on server 
successfully!", Toast.LENGTH_LONG).show();

                  }
              }
          });

回答1:


you have to create a data reference first

 DatabaseReference ref = FirebaseDatabase.getInstance().getReference("/path/");

than geofire object

        GeoFire geoFire = new GeoFire(ref);

now retreive geo firelocation within particular range like this

 GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(lat, long), radius);
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            Log.d(Const.TAG,String.format("Key %s entered the search area at [%f,%f]", key, location.latitude, location.longitude));
        }

        @Override
        public void onKeyExited(String key) {
            Log.d(Const.TAG,String.format("Key %s is no longer in the search area", key));
        }

        @Override
        public void onKeyMoved(String key, GeoLocation location) {
            Log.d(Const.TAG,String.format("Key %s moved within the search area to [%f,%f]", key, location.latitude, location.longitude));
        }

        @Override
        public void onGeoQueryReady() {
            Log.d(Const.TAG,"All initial data has been loaded and events have been fired!");
        }

        @Override
        public void onGeoQueryError(DatabaseError error) {
            Log.d(Const.TAG,"There was an error with this query: " + error);
        }
    });


来源:https://stackoverflow.com/questions/45060629/i-have-inserted-location-using-geofire-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!