java - 使用 android Geofire 获取经度和纬度

标签 java android firebase firebase-realtime-database geofire

我正在尝试使用 Android 版 Geo fire 获取用户位置。我试图获取用户的经度和纬度并将其保存在 firebase 数据库中,但我没有运气。

下面是我的代码。有人可以告诉我我做错了什么吗?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_after_registration);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

    savebutton=findViewById(R.id.save);

    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef =  FirebaseDatabase.getInstance().getReference();
    DatabaseReference update = rootRef.child("Users").child(uid);

    GeoFire geoFire=new GeoFire(rootRef);

    geoFire.getLocation("location", new LocationCallback() {
        @Override
        public void onLocationResult(String key, GeoLocation location) {
            if(location!=null){

                Double longi = location.longitude;
                Double lat = location.latitude;

                update.child("longitude").setValue(longi);
                update.child("latitude").setValue(lat);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

最佳答案

在 GeoFire 中,您可以通过字符串键设置和查询位置。要设置键的位置,只需调用 setLocation 方法:

geoFire.setLocation("location", new GeoLocation(37.7853889, -122.4056973), new GeoFire.CompletionListener() {
    @Override
    public void onComplete(String key, FirebaseError error) {
        if (error != null) {
            System.err.println("There was an error saving the location to GeoFire: " + error);
        } else {
            System.out.println("Location saved on server successfully!");
        }
    }
});

设置位置后,您可以检索它并将值保存到数据库:

geoFire.getLocation("location", new LocationCallback() {
    @Override
    public void onLocationResult(String key, GeoLocation location) {
        if (location != null) {
            System.out.println(String.format("The location for key %s is [%f,%f]", key, location.latitude, location.longitude))
// save longitude and latitude to db
            Double longi = location.longitude;
            Double lat = location.latitude
        } else {
            System.out.println(String.format("There is no location for key %s in GeoFire", key));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.err.println("There was an error getting the GeoFire location: " + databaseError);
    }
});

关于java - 使用 android Geofire 获取经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59697812/

相关文章:

angular - 在 Angular 13 中进行 Firebase 集成期间找不到名称 'PhoneOrOauthTokenResponse'

Firebase 部署错误 Enoent

java - 业务流程管理开发环境

java - 如何在写入输出流时连续监听输入流?

Android proguard 错误 com.google.ads.util.i : and setMediaPlaybackRequiresUserGesture(boolean)

android:为什么要在 Environment.getDataDirectory() 中使用 Environment 而不是用于 getFilesDir()

javascript - Firebase 控制台不显示更新的数据

java - 如何使用 Java/JDBC 将时间戳数组传递给 Postgres 函数?

java - 即使满足条件,我的 While 循环也不会结束

Android:没有正在运行的设备