简体   繁体   中英

finding nearst user using FireBase(thinking of geofire?)

Im trying to find the nearest user to each user which is online, this is my firebase database:

数据库

This is my MainActivity onCreate function which is getting the data:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    // Set default username is anonymous.
    mUsername = ANONYMOUS;
    mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
    // Initialize Firebase Auth
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    if (mFirebaseUser == null) {
        // Not signed in, launch the Sign In activity
        startActivity(new Intent(this, SignInActivity.class));
        finish();
        return;
    } else {
        mUsername = mFirebaseUser.getDisplayName();
        if (mFirebaseUser.getPhotoUrl() != null) {
            mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
        }
    }

    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, this)
        .addApi(Auth.GOOGLE_SIGN_IN_API)
        .addApi(AppInvite.API)
        .build();

    setContentView(R.layout.activity_user_profile);
    Firebase.setAndroidContext(this);
    gpsTracker = new GPSTracker(MainActivity.this);
    double lat = gpsTracker.getLatitude();
    double lang = gpsTracker.getLongitude();
    mDatabase = FirebaseDatabase.getInstance().getReference();
    mAuth = FirebaseAuth.getInstance();
    mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("latitude").setValue(lat);
    mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("longitude").setValue(lang);
    mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("Availability").setValue(true);
    mDatabase.child("User").child(mAuth.getCurrentUser().getUid()).child("Availability").onDisconnect().setValue(false);

    FirebaseOptions options = new     FirebaseOptions.Builder().setApplicationId("geofire").setDatabaseUrl(GEO_FIRE_DB).build();
    FirebaseApp app = FirebaseApp.initializeApp(this, options);
    // setup GeoFire
    this.geoFire = new     GeoFire(FirebaseDatabase.getInstance(app).getReferenceFromUrl(GEO_FIRE_REF));
}

I'm new in Android development and it's my first real application, my next mission is to use the information stored in the database so each user can get connected with the nearest user. How can I do it? I thought about creating hash with all distance of user and save it in user via SharedPreferences .

Please help me. Thank you very much.

Here's what you can do

Location currentLocation = new Location();     
currentLocation.setLatitude(CURRENT_USER_LAT); 
currentLocation.setLongitude(CURRENT_USER_LNG);
Location compareLoc = new Location();
float minDist = Float.MAX_VALUE;
float distance = Float.MAX_VALUE;
for(int i=0; i<TOTAL_LOCATIONS; i++)
{
    compareLoc.setLatitude(LOCATIONS[i].lat); 
    compareLoc.setLongitude(LOCATIONS[i].lng);
    distance = currentLocation.distanceTo(compareLoc);
    if(distance<minDist) {
        //THIS IS THE ENTRY WITH THE MINIMUM DISTANCE.
        //DO WITH IT AS YOU PLEASE
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM