gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:07:27 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com