How do I save latitude and longitude in Firebase?

Asked

Viewed 532 times

0

They told me you have to save how double, but I don’t know how to do it, If anyone can help me I appreciate :)

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    Marker marker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // 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);
    }

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        googleMap.setMyLocationEnabled(true); //Exibi o botão de localizar a localização do usuário

        mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

            @Override
            public void onMapLongClick(LatLng arg0) {
                if (marker != null) {
                    marker.remove();
                }
                marker = mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.code_the_road_small))
                        .position(
                                new LatLng(arg0.latitude,
                                        arg0.longitude))
                        .draggable(true).visible(true));
                 }


        });}}

How do I save latitude and longitude in Firebase?

1 answer

1

If you wish in the future to conduct a ray-based search, I recommend you use the Geofire.

Example:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("path/to/geofire");
GeoFire geoFire = new GeoFire(ref);

geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));

Or you can save a double value, example:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("path/to/user");
ref.child("latitude").setValue(37.7853889);
ref.child("longitude").setValue(-122.4056973);

Firebase allows you to save data in various ways, recommend you consult their documentation as well and that is in Portuguese.

https://firebase.google.com/docs/database/android/save-data?hl=pt-br

Browser other questions tagged

You are not signed in. Login or sign up in order to post.