I created the database in Firebase and tried to show the marker in the map. Still not working. what should I do. The app runs but when map activity is opened the app closes. I creates the database manually. I referred this link :- Retrieve location from Firebase and put marker on google map api for android.
This is the code
Map Activity
map.java
import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.FragmentActivity; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdate; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.lang.reflect.Array; import java.util.EventListener; public class map extends FragmentActivity implements OnMapReadyCallback { FirebaseDatabase mDatabase; DatabaseReference mDatabaseReference; String nameddd; private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); nameddd = FirebaseDatabase.getInstance().getReference("Location").child("Name").toString(); mDatabase = FirebaseDatabase.getInstance(); mDatabaseReference = mDatabase.getReference("Location"); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mDatabaseReference.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { LatLng newLocation = new LatLng(dataSnapshot.child("Latitude").getValue(Long.class),dataSnapshot.child("Longitude").getValue(Long.class)); mMap.addMarker(new MarkerOptions().position(newLocation).title(nameddd) ); } @Override public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { } @Override public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { } @Override public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); }}
This is the Firebase database structure
Location |-2JHHhnUhYKVzAmoDvrhtUJfhGEg2 |- ABpos: "20" |- Latitude: "12" |- Longitude: "22" |- Name: "Sion"
Logcat
Process: com.bloodapp.blood, PID: 5393 com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertLong(com.google.firebase:firebase-database@@19.2.1:384) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database@@19.2.1:295) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:214) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79) at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203) at com.bloodapp.blood.map$1.onChildAdded(map.java:54) at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:79) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Advertisement
Answer
You are getting the following error:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
Because you are trying to get the value of the Latitude
property as a Long
:
dataSnapshot.child("Latitude").getValue(Long.class)
While in your database is stored as a String.
|- Longitude: "22"
See the equation marks?
To solve this, you should change the type for your Latitude
and Longitude
properties in the database to be of type double
and not String:
|- Longitude: 22.01
And get them accordingly:
double lat = dataSnapshot.child("Latitude").getValue(Double.class)
The latitude and longitude cannot be long
numbers, it should be double
and this is because both contain decimals.