我正在保存用户发布的帖子的坐标。我正在生成一个push id,然后使用它保存post和geofire坐标的数据。
我只想展示那些方圆0.5公里以内的柱子。我也在使用GeoFire
库,但无法完成任务。
下面是我生成推送ID的方法:itemID = databaseReferenceRequests.push().getKey();
以下是我如何使用它来保存geofire坐标以及帖子的数据:
geoFire.setLocation(itemID,
new GeoLocation(Double.parseDouble(currentLat.getText().toString()),
Double.parseDouble(currentLng.getText().toString())));
databaseReferenceRequests.child(itemID).setValue(hRequest);
它是这样被保存的:
问题是,当我试图只获取距离我所能到达的0.5公里内的那些帖子时,它不会发生,所有的帖子无论是近的还是远的都会被检索到。
以下是我如何检索的:
public void retrieveHelpRequests() {
geoQuery = geoFire.queryAtLocation(new GeoLocation(currentLatDouble, currentLngDouble), 0.5);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
databaseReference.child("help-requests").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
imageUID = newRequest.get("imageUIDh");
homelessDescription = newRequest.get("homelessDescription");
currentLat = newRequest.get("currentLat");
currentLng = newRequest.get("currentLng");
postedBy = newRequest.get("postedBy");
postedAtTime = newRequest.get("postedAtTime");
postedOnDate = newRequest.get("postedOnDate");
utcFormatDateTime = newRequest.get("utcFormatDateTime");
String timeStr = utcFormatDateTime;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = null;
try {
// error on line below
date = df.parse(timeStr);
} catch (ParseException e) {
e.printStackTrace();
}
df.setTimeZone(TimeZone.getDefault());
final String persisted = df.format(date);
// Parse string from DB - UTC timezone
Date parsed = null;
try {
parsed = df.parse(persisted);
} catch (ParseException e) {
e.printStackTrace();
}
// Now convert to whatever timezone for display purposes
final SimpleDateFormat displayFormat = new SimpleDateFormat("h:mm a");
displayFormat.setTimeZone(TimeZone.getDefault());
formattedTime = displayFormat.format(parsed);
prepareDataForRequests();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, databaseError.getMessage(), Snackbar.LENGTH_LONG);
snackbar.setDuration(Snackbar.LENGTH_SHORT);
snackbar.show();
// helpRequestsLoadingDialog.dismiss();
progressBarLoadingRequests.setVisibility(View.INVISIBLE);
}
});
databaseReference.child("help-requests").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
// adView.loadAd(request);
card_ads2.setVisibility(View.VISIBLE);
adView2.loadAd(request2);
if (snackbar != null) {
snackbar.dismiss();
}
progressBarLoadingRequests.setVisibility(View.INVISIBLE);
if (fastItemAdapter.getAdapterItemCount() == 0) {
emptyRVtext.setVisibility(View.VISIBLE);
emptyRVtexthh.setVisibility(View.VISIBLE);
card_ads2.setVisibility(View.INVISIBLE);
} else {
emptyRVtext.setVisibility(View.INVISIBLE);
emptyRVtexthh.setVisibility(View.INVISIBLE);
}
// progressBarLoadingRequests.setVisibility(View.INVISIBLE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, databaseError.getMessage(), Snackbar.LENGTH_LONG);
snackbar.setDuration(Snackbar.LENGTH_SHORT);
snackbar.show();
// hRequestsLoadingDialog.dismiss();
progressBarLoadingRequests.setVisibility(View.INVISIBLE);
}
});
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
Toast.makeText(getBaseContext(), "Error retriving geoquery", Toast.LENGTH_SHORT).show();
}
});
}
错误如下:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
请告诉我如何只检索那些在0.5公里以内的帖子?
最佳答案
您只需更改queryAtLocation()
的第二个参数
double radius = 0.5; // in km
geoQuery = geoFire.queryAtLocation(new GeoLocation(currentLatDouble, currentLngDouble), radius);
是否确实检索到距用户0.5公里以上的所有项目?
下面是如何验证它们
Location userLocation = new Location("userLocation");
userLocation.setLatitude(currentLatDouble);
userLocation.setLongitude(currentLngDouble);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
Location itemLocation = new Location("itemLocation");
itemLocation.setLatitude(location.latitude);
itemLocation.setLongitude(location.longitude);
Log.d("distance to " + key" is", userLocation.distanceTo(itemLocation) + "");
}