I have 3 sets of latitude and longitude stored in FirebaseDatabase
.
What I am doing is I am retrieving these stored sets and showing location address using them.
Here's my code:
databaseReference.child("child").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
currentLat = newRequest.get("currentLat");
currentLng = newRequest.get("currentLng");
currentLatDouble = Double.parseDouble(currentLat);
currentLngDouble = Double.parseDouble(currentLng);
Intent intent = new Intent(MainActivity.this, GeocodeAddressIntentService.class);
intent.putExtra(RECEIVER, mResultReceiver);
intent.putExtra(LOCATION_LATITUDE_DATA_EXTRA, currentLatDouble);
intent.putExtra(LOCATION_LONGITUDE_DATA_EXTRA, currentLngDouble);
startService(intent);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here's GeocodeAddressIntentService.java
file's code:
public class GeocodeAddressIntentService extends IntentService {
public static final int SUCCESS_RESULT = 0;
public static final int FAILURE_RESULT = 1;
public static final String PACKAGE_NAME =
"com.abc.xyz";
public static final String RESULT_DATA_KEY = PACKAGE_NAME + ".RESULT_DATA_KEY";
public static final String RESULT_ADDRESS = PACKAGE_NAME + ".RESULT_ADDRESS";
public static final String LOCATION_LATITUDE_DATA_EXTRA = PACKAGE_NAME + ".LOCATION_LATITUDE_DATA_EXTRA";
public static final String LOCATION_LONGITUDE_DATA_EXTRA = PACKAGE_NAME + ".LOCATION_LONGITUDE_DATA_EXTRA";
public static final String RECEIVER = PACKAGE_NAME + ".RECEIVER";
protected android.support.v4.os.ResultReceiver resultReceiver;
private static final String TAG = "GEO_ADDY_SERVICE";
Geocoder geocoder;
List<Address> addresses = null;
Location location;
public GeocodeAddressIntentService() {
super("GeocodeAddressIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG, "onHandleIntent");
double latitude = intent.getDoubleExtra(LOCATION_LATITUDE_DATA_EXTRA, 0);
double longitude = intent.getDoubleExtra(LOCATION_LONGITUDE_DATA_EXTRA, 0);
StringBuilder result = new StringBuilder();
try {
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
result.append(address.getLocality()).append("\n");
result.append(address.getCountryName());
}
resultReceiver = intent.getParcelableExtra(RECEIVER);
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<>();
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
deliverResultToReceiver(SUCCESS_RESULT,
address.getLocality() + ", " + address.getCountryName(), address);
} catch (IOException e) {
Log.e("tag", e.getMessage());
deliverResultToReceiver(FAILURE_RESULT, e.getMessage(), null);
}
}
private void deliverResultToReceiver(int resultCode, String message, Address address) {
Bundle bundle = new Bundle();
bundle.putParcelable(RESULT_ADDRESS, address);
bundle.putString(RESULT_DATA_KEY, message);
if (resultReceiver != null) {
resultReceiver.send(resultCode, bundle);
} else {
Toast.makeText(getBaseContext(), "Some error occurred. Try restarting the app.", Toast.LENGTH_LONG).show();
}
}
}
What's going wrong is that the name of location is not getting shown according to every sets of latitude and longitude but according to the last/latest saved set of latitude and longitude.
Please let me know how can I show location address according to every set of latitude and longitude retrieved from the database?