2016-08-14 63 views
0

我試着去尋找最近的用戶是在線的每個用戶, 這是我的火力點數據庫(geofire的想法?):發現使用火力nearst用戶

database

這是我MainActivityonCreate功能這是越來越的數據:

@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)); 
} 

我在Android開發新的,這是我第一次真正的應用程序,我的下一個任務是使用存儲在數據庫中,這樣每個用戶CA信息ñ與最近的用戶聯繫。我該怎麼做?我想創建與用戶的所有距離的散列,並通過SharedPreferences保存在用戶。

請幫幫我。 非常感謝。

+0

既然你已經考慮使用GeoFire的,我建議您查看文檔:https://github.com/firebase/geofire-java#getting-started-與-火力點。它會告訴你如何[將用戶插入到Geoindex中](https://github.com/firebase/geofire-java#setting-location-data)(實際上這只是Firebase數據庫中的一個單獨的節點,它具有用戶鍵和Geohash)以及如何[查詢附近的用戶](https://github.com/firebase/geofire-java#geo-queries)。 –

回答

0

這裏是你可以做什麼

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 
    } 
} 
+0

我有幾個問題,1。在新的位置(),我需要插入「」正確?(它需要一個字符串),2.CURRENT_USER_LAT suppost是我的代碼的緯度變量? ,我如何通過我的Firebase數據獲得TOTAL_LOCATIONS?(似乎是我需要創建的列表?如何?) – secret

+0

1.是啊!你需要給它一個字符串。考慮給它一些字符串,如「位置1」和「比較位置」或類似的東西。實際上,這個字符串完全沒有意義。 2.是 3.不知道你是如何從FireBase返回位置的,但我認爲它應該是一個相當直接的查詢。 最後,當談到這一點時,需要考慮一些事情:想想Facebook如何做到這一點,以便找出最接近的人。他們不能!它太龐大了,它將需要永遠在用戶的設備上計算!因此,我不建議你在客戶端做。嘗試服務方面。 – meedz