I have declared the Location source and destination as global variable. But though I am not getting the right value in dist. What should I do pass the values of location outside of geoFire.getLocation() mehtod? Or how may I get distance between two geolocation by geofire?
I have tried by making the source and the destination static. But it didn't work.
public double get_distance(String from, String to)
{
source = new Location("to");
destination = new Location("form");
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
GeoFire geoFire = new GeoFire(databaseReference);
geoFire.getLocation(from, new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
source.setLatitude(location.latitude);
source.setLongitude(location.longitude);
Log.e("source latlong b4 ",source.getLatitude()+" .. "+source.getLongitude()); // the right value is passed
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("source latlong ",source.getLatitude()+" .. "+source.getLongitude()); //value hase turned into 0.0
geoFire.getLocation(to, new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
destination.setLatitude(location.latitude);
destination.setLongitude(location.longitude);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.e("destination latlong", destination.getLatitude()+" .. "+destination.getLongitude());
dist= source.distanceTo(destination);
return dist;
}