Problem:
I can store my location coordinates(lat and long) on my Firebase database using HashMap, but I don't know how to call the HashMap value onto onDataChanged method for DataSnapshot.
What's Working:
I have print the values of latitude and longitude on my logcat and my HashMap has the value. Below is my SmsReceiver.class which reads incoming messages, and save these data to Firebase database.
My MapActivity.class is currently showing default coordinates to show on map. I am confused on how I am going to access the HashMap value from the SmsReceiver.class to display a marker on GoogleMap.
What I have tried:
public void sendDataToFirebase(SmsMessage sms, Context context) {
FirebaseApp.initializeApp(context);
databaseLocation = FirebaseDatabase.getInstance().getReference("database");
String senderNumber = sms.getOriginatingAddress();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String message = sms.getMessageBody();
String[] separatedSMS = message.split("\\s+");
DatabaseReference mRef = databaseLocation.push();
double latitudedb = Double.parseDouble(separatedSMS[1]);
double longitudedb = Double.parseDouble(separatedSMS[3]);
HashMap<String,Double> coordinates = new HashMap<String,Double>();
coordinates.put("latitude", latitudedb);
coordinates.put("longitude", longitudedb);
mRef.setValue(coordinates);
coordinates.get("latitude");
coordinates.get("longitude");
mRef.getKey();
System.out.println(coordinates.get("latitude"));
System.out.println(coordinates.get("longitude"));
}
This is my MapActivity.class.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMinZoomPreference(15.0f);
mMap.setMaxZoomPreference(20.0f);
final LatLng putatan = new LatLng(14.397420, 121.033051);
final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference("conilocationdata");
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
marker = mMap.addMarker(new MarkerOptions()
.position(putatan)
.title("Here")
.flat(true));
mMap.moveCamera(CameraUpdateFactory.newLatLng(putatan));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mRef.orderByKey().limitToLast(1).addValueEventListener(postListener);