如果附近没有找到用户,GeoFire会在源位置放置另一个标记

时间:2017-02-03 21:04:04

标签: android google-maps firebase firebase-realtime-database geofire

我正在制作一个搭载Android应用并使用firebase和geofire。当我按下“查找Sameway Rides”按钮时,它会向我显示在我附近半径700米的用户。但是当没有用户时,它会在我的位置(粉红色标记)返回另一个标记(绿色标记)。我不知道我的代码有什么问题。我尝试了很多,但问题保持不变。我甚至要求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"
  }

1 个答案:

答案 0 :(得分:1)

好的,所以我想出了如何解决问题

 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();

        }

它就像魔法一样。也许它不是完美的解决方案,但它有助于达到目的。