我已将 Location 源和目标声明为全局变量。但是虽然我没有在 dist 中得到正确的值。我应该如何在 geoFire.getLocation() 方法之外传递位置值?或者我如何通过geofire获得两个地理位置之间的距离?
我尝试将源和目标设为静态。但它没有用。
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;
}
弑天下
相关分类