这是我的代码,用于查看android当前位置的纬度和经度。我想将位置保存在Firebase中。我删除了代码以查看位置,从而将位置存储在数据库中。我想将当前位置保存到Firebase中,然后再检索。
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);
}
}
答案 0 :(得分:0)
首先,向firebase控制台注册并获取JSON数据,然后您可以指向数据库引用及其对应的子代。
if(location!=null)
{
DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Location").child("Lat").setValue(location.latitude);
}