I am trying to retrieve values from geoquery. The values are getting added to the map but when I return the values the map is empty.
Here myPosition
is my position Latlng values that I am using for my location.
i have edited the question but still the problem is occuring the map is still empty
private Map<String, GeoLocation> geoMap;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate
(R.layout.fragment_new_search2, container, false);
geoMap= new HashMap<>();
readdata(new MyCallback() {
@Override
public void onCallback(Map<String,
GeoLocation> geomap) {
// int size = geomap.size();
}
});
int size = geoMap.size();
Toast.makeText(getContext(),
String.valueOf(size), Toast.LENGTH_SHORT).show();
return view;
}
private void readdata(final MyCallback myCallback){
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference()
.child("WorkLocation");
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(myPosition.latitude, myPosition.longitude), 2);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
geoMap.put(key,location);
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
myCallback.onCallback(geoMap);
}
public interface MyCallback {
void onCallback(Map<String, GeoLocation> geomap);
}
I tried a toast inside onkeyentered
the values are there and they are getting added but still, the values are not returning to the method and the size of the map is still zero.
All code that requires the data to have been loaded, must be inside (or called from inside) the on...
method. So:
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference()
.child("WorkLocation");
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(myPosition.latitude, myPosition.longitude), 2);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
geoMap.put(key,location);
myCallback.onCallback(geoMap);
}
@Override
public void onKeyExited(String key) { }
@Override
public void onKeyMoved(String key, GeoLocation location) { }
@Override
public void onGeoQueryReady() { }
@Override
public void onGeoQueryError(DatabaseError error) { }
});
}
If you only want your onCallback
to be invoked once all initial keys have been loaded through Geofire, put it in onGeoQueryReady
:
DatabaseReference ref =
FirebaseDatabase.getInstance().getReference()
.child("WorkLocation");
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(myPosition.latitude, myPosition.longitude), 2);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
geoMap.put(key,location);
}
@Override
public void onKeyExited(String key) { }
@Override
public void onKeyMoved(String key, GeoLocation location) { }
@Override
public void onGeoQueryReady() {
myCallback.onCallback(geoMap);
}
@Override
public void onGeoQueryError(DatabaseError error) { }
});
}