2016-08-14 19 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