Skip to content Skip to sidebar Skip to footer

Retrieve Location From Firebase And Put Marker On Google Map

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 crea

Solution 1:

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.

Solution 2:

@OverridepublicvoidonMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    DatabaseReferencerootRef= FirebaseDatabase.getInstance().getReference();
    DatabaseReferencelocationsRef= rootRef.child("childName");
    finalGeoFiregeoFire=newGeoFire(locationsRef);
        geoFire.getLocation("DataKey", newLocationCallback() {
            @OverridepublicvoidonLocationResult(String key, GeoLocation location) {
                doublelat=location.latitude;
                doublelng=location.longitude;
                LatLngsydney=newLatLng(lng, lat);
                mMap.addMarker(newMarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(HUE_BLUE)).position(sydney).title("car"));
                Toast.makeText(getApplicationContext(),""+sydney,Toast.LENGTH_SHORT).show();
                mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(9));
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });
    

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap = googleMap;
    mMap.setMyLocationEnabled(true);
    buildGoogleApiClient();

}

Post a Comment for "Retrieve Location From Firebase And Put Marker On Google Map"