I have a class MapActivity.java
with method which search Cafes near to me and second method
private void parseLocationResult(JSONObject result) throws JSONException {
String placeName = "", ivicinity = "";
Double placeRating = 0.0;
double latitude, longitude;
JSONArray jsonArray = result.getJSONArray("results");
if (result.getString(STATUS).equalsIgnoreCase(OK)) {
mMap.clear();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject place = jsonArray.getJSONObject(i);
if (!place.isNull(NAME)) {
placeName = place.getString(NAME);
}
if (!place.isNull(VICINITY)) {
ivicinity = place.getString(VICINITY);
}
if(!place.isNull( RATING )){
placeRating = place.getDouble( RATING );
}
latitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION)
.getDouble(LATITUDE);
longitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION)
.getDouble(LONGITUDE);
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(latitude, longitude);
markerOptions.position(latLng);
markerOptions.title(placeName + " : " + ivicinity);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
PlaceDetail cafe= new PlaceDetail(placeName,ivicinity,placeRating);
Map<String, String> dataMap = new HashMap<>();
dataMap.put("name", placeName);
dataMap.put("address", ivicinity);
dataMap.put("rating", String.valueOf(placeRating));
db.collection("collection")
.add(dataMap)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
}
});
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12f));
mMap.addMarker(markerOptions);
}
Toast.makeText(getBaseContext(), jsonArray.length() + " Cafe(s) Found!",
Toast.LENGTH_LONG).show();
} else if (result.getString(STATUS).equalsIgnoreCase(ZERO_RESULTS)) {
Toast.makeText(getBaseContext(), "No Cafe Found in 4 Miles Radius!!!",
Toast.LENGTH_LONG).show();
}
}
which save my search results to Firestore. Saving to base works almost fine, but there is one problem. I would like to save only Cafes which are not already added. I think I need any check system - if there is already data with the same values as I found, don't save it, in other case save.
How to block the addition of data already stored there?