【问题标题】:Get longitude and latitude using android Geofire使用 android Geofire 获取经度和纬度
【发布时间】:2020-04-29 01:20:49
【问题描述】:

我正在尝试使用 Geo fire for android 获取用户位置。我试图获取用户的经度和纬度并将其保存在 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) {

        }
    });

【问题讨论】:

  • 错误是什么
  • @PeterHaddad 我没有收到任何错误,只是数据库中没有显示经度和纬度
  • @GCode22 节点的结构可能不像预期的那样。

标签: java android firebase firebase-realtime-database geofire


【解决方案1】:

在 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);
    }
});

【讨论】:

  • 添加日志并查看经度值
  • 我没有看到它@Peter
  • 那就不进入回调LocationCallback()
  • 我该如何解决这个问题。我对使用 geofire 有点陌生。你能帮我看看吗
  • 你是先用setLocation设置位置的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2016-09-29
  • 1970-01-01
相关资源
最近更新 更多