したがって、GeoFire(src:https://github.com/firebase/geofire-java)というライブラリを使用して、特定の距離内でユーザーから投稿を取得しました。私のコードでは、私はこのような方法を作った。GeoQueryを使用したデータの取得
private HashMap<String, GeoLocation> getPostAroundUser(DatabaseReference rootRef) {
final HashMap<String, GeoLocation> postHashMapLocation = new HashMap<String, GeoLocation>();
DatabaseReference refPostGeo = rootRef.child("geo").child("posts");
final GeoFire geoFire = new GeoFire(refPostGeo);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(mLatitude, mLongitude), 3);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Log.i(TAG, "Post with key: " + key + " was added to hash with location (lat, long): (" + location.latitude + ", " + location.longitude + ")");
postHashMapLocation.put(key, location);
Log.i(TAG, "number of entry after added " + postHashMapLocation.size());
}
@Override
public void onKeyExited(String key) {
Log.i(TAG, "Post with key: " + key + " was removed");
postHashMapLocation.remove(key);
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
Log.i(TAG, "number of entry onGeoQueryReady " + postHashMapLocation.size());
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
Log.i(TAG, "total entry in function: " + postHashMapLocation.size()); //postHashMapLocation.size() returns 0
return postHashMapLocation;
}
しかし、この方法では空HashMap
を返しますので、私は何が起こっているかを確認するためにいくつかのログを追加し、ここでログは次のとおりです。
02-21 18:09:32.609 15115-15115/com.example.company.app I/ExampleActivity: total entry in function: 0
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7DPzEZHSdGHqfbmna was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 1
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7DULwW01S9-haorm9 was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 2
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7Dt6uvs50qddJHxiA was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 3
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -Kd7EOF41MijwSfrxpX1 was added to hash with location (lat, long): (-6.1459048, 106.692038)
02-21 18:09:32.839 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 4
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB3mJvB0K6nSCtNnoL was added to hash with location (lat, long): (-6.1333898, 106.6861029)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 5
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB3r13H7y3jiovlLdi was added to hash with location (lat, long): (-6.1333898, 106.6861029)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 6
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: Post with key: -KdB58ArIOFKHrfEnYpr was added to hash with location (lat, long): (-6.1335054, 106.6910521)
02-21 18:09:32.849 15115-15115/com.example.company.app I/ExampleActivity: number of entry after added 7
02-21 18:09:32.869 15115-15115/com.example.company.app I/ExampleActivity: number of entry onGeoQueryReady 7
それはgeoQuery
のログの前に起動したHashMap
を返す前に私が入れたログを示しています。だからこそメソッドが空のまま返るのですHashMap
私の質問は、をonKeyEntered
からこのメソッドで返すにはどうすればいいですか?ログからわかるように、HashMap
はonKeyPressed
の内部で更新されましたが、geoQuery
の外部には取得できません。
注:ここでも同様の問題が見つかりましたError performing functions inside a GeoFire and Firebase query、しかし答えはありません。
私は例を教えてくれますか? – YorS
これは実際にアプリケーションに依存し、データで何をしたいのかによって異なります。一般的な例は、あなたの 'RecyerlView'アダプタか、おそらくGoogleマップをそれらのコールバックから更新することです。 –