I want to store latitude and longitude in realtime FIrebase…And have the following code

问题: This is my code to view latitude and longitude of current location in android. I want to save the location in firebase. I have removed the code to view the location thus to...

问题:

This is my code to view latitude and longitude of current location in android. I want to save the location in firebase. I have removed the code to view the location thus to store location in database. I want to save the current location to firebase then retrieve it later.

public class MapsActivity extends AppCompatActivity {

    private GoogleMap mMap;
    private FusedLocationProviderClient client;
    private LatLng lng;

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

requestPermission();
        client = LocationServices.getFusedLocationProviderClient(this);

        Button button = findViewById(R.id.button);
        EditText EditText  = findViewById(R.id.ed);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityCompat.checkSelfPermission(MapsActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                client.getLastLocation().addOnSuccessListener(MapsActivity.this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        if(location!=null)
                        {


                        }
                    }
                });
            }
        });

    }
public void requestPermission()
{

ActivityCompat.requestPermissions(this,new String[]{ACCESS_FINE_LOCATION},1);

}

}

回答1:

First, register with firebase Console and get the JSON data after that you can point the Database Reference and their corresponding child.

if(location!=null)
{
    DatabaseReference mDatabase;
    mDatabase = FirebaseDatabase.getInstance().getReference();
    mDatabase.child("Location").child("Lat").setValue(location.latitude);
}
  • 发表于 2019-03-01 03:04
  • 阅读 ( 191 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除