如果附近没有发现用户,GeoFire 会在源位置放置另一个标记
GeoFire putting another marker on the source location if no user is found nearby
我正在制作拼车 android 应用程序并使用 firebase 和 geofire。当我按下按钮 "Find Sameway Rides" 时,它会显示我附近 700 米半径内的用户。但是当没有用户时,它会在我的位置(粉红色标记)returns 另一个标记(绿色标记)。我不知道我的代码有什么问题。我尝试了很多,但问题仍然存在。我什至向 Firebase 支持寻求帮助,但他们无法纠正它。所以请帮忙。
变量。你需要知道的
private DatabaseReference mDatabase;
//for geofire
private DatabaseReference geoDatabase;
public GeoFire geoFire;
public GeoQuery geoQuery;
// [START initialize_database_ref]
mDatabase = FirebaseDatabase.getInstance().getReference();
// [END initialize_database_ref]
geoDatabase = mDatabase.child("/Ridedata/Geodatafrom/");
geoFire = new GeoFire(geoDatabase);
现在点击按钮会发生什么
// post data to server and show nearby rides
public void nowFindRide(View view) throws JSONException {
//Sending user info to database...
final String from = eetsource.getText().toString();
final String to = eetdestination.getText().toString();
final String when = leavetext.getText().toString();
final String name = user_name.getText().toString();
final String email = user_email.getText().toString();
final String picture = profile_pic_url.getString("url");
userAuth = FirebaseAuth.getInstance();
authCurrentUser = userAuth.getCurrentUser();
final String uKey = authCurrentUser.getUid();
double fromLat = currentLatLng.latitude;
double fromLong = currentLatLng.longitude;
if (fromLat == 0.0 && fromLong == 0.0) {
fromLat = sourcelat;
fromLong = sourcelong;
} else {
fromLat = currentLatLng.latitude;
fromLong = currentLatLng.longitude;
}
final long uid = Id;
// post data to server.
final String uniqueKey = mDatabase.child("Ridedata").push().getKey();
LocationData locationData = new LocationData(name, from, to, when, picture, email);
Map<String, Object> dataValues1 = locationData.toMap();
final Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/Ridedata/" + "/Ridedetail/" + uKey, dataValues1);
geoFire.setLocation(uKey, new GeoLocation(fromLat, fromLong));
geoQuery = geoFire.queryAtLocation(new GeoLocation(fromLat, fromLong), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
geoDatabase.child("Geodatafrom").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG,"data change:"+ dataSnapshot);
// One nearbyUser = dataSnapshot.getValue(One.class);
setNearbyMarker(new LatLng(location.latitude,location.longitude));
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG,"the read failed: " + databaseError.getMessage());
}
});
}
@Override
public void onKeyExited(String key) {
Log.d(TAG,"Key %s is no longer in the search area" + key);
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
Log.d(TAG,String.format("Key %s moved within the search area to [%f,%f]",
key, location.latitude, location.longitude));
}
@Override
public void onGeoQueryReady() {
Log.d(TAG,"All initial data has been loaded and events have been fired!");
}
@Override
public void onGeoQueryError(DatabaseError error) {
Log.d(TAG,"There was an error with this query: " + error);
}
});
mDatabase.updateChildren(childUpdates);
// done flushing data into database.
}
这是我的数据库
Firebase database for my project
以及问题截图 Screenshot showing the problem
Geofire 规则
"Geofire": {
".indexOn": "g"
}
好的,我想出了解决问题的方法
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
if (key != uKey ){
setNearbyMarker(new LatLng(location.latitude,location.longitude),key);
}
// markerDestination.add(key);
}
@Override
public void onKeyExited(String key) {
Log.d(TAG,"Key %s is no longer in the search area" + key);
Sorrydialog sorrydialog = new Sorrydialog();
sorrydialog.showDialog(BliMaps.this);
markerDestination.remove();
}
它的工作方式与 magic.Maybe 一样,它不是完美的解决方案,但它达到了目的。
我正在制作拼车 android 应用程序并使用 firebase 和 geofire。当我按下按钮 "Find Sameway Rides" 时,它会显示我附近 700 米半径内的用户。但是当没有用户时,它会在我的位置(粉红色标记)returns 另一个标记(绿色标记)。我不知道我的代码有什么问题。我尝试了很多,但问题仍然存在。我什至向 Firebase 支持寻求帮助,但他们无法纠正它。所以请帮忙。
变量。你需要知道的
private DatabaseReference mDatabase;
//for geofire
private DatabaseReference geoDatabase;
public GeoFire geoFire;
public GeoQuery geoQuery;
// [START initialize_database_ref]
mDatabase = FirebaseDatabase.getInstance().getReference();
// [END initialize_database_ref]
geoDatabase = mDatabase.child("/Ridedata/Geodatafrom/");
geoFire = new GeoFire(geoDatabase);
现在点击按钮会发生什么
// post data to server and show nearby rides
public void nowFindRide(View view) throws JSONException {
//Sending user info to database...
final String from = eetsource.getText().toString();
final String to = eetdestination.getText().toString();
final String when = leavetext.getText().toString();
final String name = user_name.getText().toString();
final String email = user_email.getText().toString();
final String picture = profile_pic_url.getString("url");
userAuth = FirebaseAuth.getInstance();
authCurrentUser = userAuth.getCurrentUser();
final String uKey = authCurrentUser.getUid();
double fromLat = currentLatLng.latitude;
double fromLong = currentLatLng.longitude;
if (fromLat == 0.0 && fromLong == 0.0) {
fromLat = sourcelat;
fromLong = sourcelong;
} else {
fromLat = currentLatLng.latitude;
fromLong = currentLatLng.longitude;
}
final long uid = Id;
// post data to server.
final String uniqueKey = mDatabase.child("Ridedata").push().getKey();
LocationData locationData = new LocationData(name, from, to, when, picture, email);
Map<String, Object> dataValues1 = locationData.toMap();
final Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/Ridedata/" + "/Ridedetail/" + uKey, dataValues1);
geoFire.setLocation(uKey, new GeoLocation(fromLat, fromLong));
geoQuery = geoFire.queryAtLocation(new GeoLocation(fromLat, fromLong), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
geoDatabase.child("Geodatafrom").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG,"data change:"+ dataSnapshot);
// One nearbyUser = dataSnapshot.getValue(One.class);
setNearbyMarker(new LatLng(location.latitude,location.longitude));
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG,"the read failed: " + databaseError.getMessage());
}
});
}
@Override
public void onKeyExited(String key) {
Log.d(TAG,"Key %s is no longer in the search area" + key);
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
Log.d(TAG,String.format("Key %s moved within the search area to [%f,%f]",
key, location.latitude, location.longitude));
}
@Override
public void onGeoQueryReady() {
Log.d(TAG,"All initial data has been loaded and events have been fired!");
}
@Override
public void onGeoQueryError(DatabaseError error) {
Log.d(TAG,"There was an error with this query: " + error);
}
});
mDatabase.updateChildren(childUpdates);
// done flushing data into database.
}
这是我的数据库 Firebase database for my project 以及问题截图 Screenshot showing the problem
Geofire 规则
"Geofire": {
".indexOn": "g"
}
好的,我想出了解决问题的方法
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
if (key != uKey ){
setNearbyMarker(new LatLng(location.latitude,location.longitude),key);
}
// markerDestination.add(key);
}
@Override
public void onKeyExited(String key) {
Log.d(TAG,"Key %s is no longer in the search area" + key);
Sorrydialog sorrydialog = new Sorrydialog();
sorrydialog.showDialog(BliMaps.this);
markerDestination.remove();
}
它的工作方式与 magic.Maybe 一样,它不是完美的解决方案,但它达到了目的。