【发布时间】:2020-05-02 19:20:47
【问题描述】:
我知道这个问题被问了一遍又一遍,但我似乎无法得到一个好的答案。我正在尝试根据用户位置检索数据。例如,假设我的数据库有一个面包店列表。每个面包店都有一个地址,经纬度,邮政编码,城市,州。现在我想向顾客展示所有的面包店,但只有那些在他们附近的面包店让我们说 5 英里外我将如何去做。过去几天我一直在寻找答案,但似乎没有任何效果。我什至尝试了this 回答我在堆栈溢出上看到的并且它不起作用。请有人帮我解决这个问题。下面是我的代码
//这个页面是用户注册后会自动保存经纬度和Geohash等的地方
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(final Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
Double latittude = location.getLatitude();
Double longitude = location.getLongitude();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Users");
DatabaseReference update = rootRef.child(uid);
GeoFire geoFire=new GeoFire(rootRef);
geoFire.setLocation("latandlong", new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError 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("latandlong", 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
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference update = rootRef.child("Users").child(uid);
Double longi = location.longitude;
Double lat = location.latitude;
update.child("latandlong").setValue(location.latitude+ "," +location.longitude);
//update.child("longitude").setValue(longi);
//update.child("latitude").setValue(lat);
} 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);
}
});
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
menuItem=(MenuItem)findViewById(R.id.item_sign_in);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
recycler=findViewById(R.id.recyclerView);
recycler.setLayoutManager(new LinearLayoutManager(this));
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
Double lat=location.getLatitude();
Double longi=location.getLongitude();
firebaseDatabase=FirebaseDatabase.getInstance().getReference("Users");
GeoFire geoFire=new GeoFire(firebaseDatabase.child("latandlong"));
GeoQuery geoQuery=geoFire.queryAtLocation(new GeoLocation(lat,longi),5);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
firebaseDatabase.child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
list=new ArrayList<UserInformation>();
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
UserInformation uuu=dataSnapshot1.getValue(UserInformation.class);
list.add(uuu);
}
}
adapter=new MyAdapter(NewsFeedActivity.this,list);
recycler.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
});
【问题讨论】:
-
既然您已经在使用 GeoFire,那么您就有了适合这项工作的工具。您当前的代码有什么问题?具体来说:当您在调试器中运行此代码时,哪一行没有按照您的预期执行?
-
@FrankvanPuffelen 非常感谢您的回复。当我运行我的代码时,我没有收到任何错误。我得到一个空白页。没有结果显示
-
在每一步都添加
Logs,看看您在哪一部分没有得到预期的结果,如果您感到困惑,请使用Logs更新您的问题 -
@GCode22 你需要做的还不止这些。如果你在
GeoQuery geoQuery=geoFire.queryAtLocation(上设置一个断点,它会被执行吗?lat和longi的值是多少?如果您在onKeyEntered内的第一行设置断点并运行,那么该行是否会被执行?有了key? -
@FrankvanPuffelen 没有执行任何操作。 longi 取当前经度,lat 取当前纬度
标签: android firebase firebase-realtime-database geofire