问题
I'm making an app with firebase and google maps But I'm implementing this:
In the AddActivity activity, you set the latitude and longitude by clicking on a certain part of the map by placing a marker When you press the "send" button these data are sent to the firebase
And I would like to rescue the data of that marker that was sent as the title, And that it is placed in the map of the MainActivity with the latitude and longitude that is in the firebase
This is how my database is organized
I need to get the coordinates of the database, and with those coordinates a marker is placed in the exact place
the relevant code:
the addActivity method that send the information
private void sendPostEvent(){
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".",",")).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
final String postuid = FirebaseUtils.getUid();
String organizadoPor = etOrganizadoPor.getText().toString();
String tituloEvento = etTituloEvento.getText().toString();
String descripcionEvento = etDescripcion.getText().toString();
String costoEvento = etCosto.getText().toString();
mPost.setOrganizadoPor(organizadoPor);
mPost.setMes(mPost.getMes());
mPost.setDia(mPost.getDia());
mPost.setHora(mPost.getHora());
mPost.setMinutos(mPost.getMinutos());
mPost.setTitulo(tituloEvento);
mPost.setDescripicion(descripcionEvento);
mPost.setCosto(costoEvento);
mPost.setLatitud(mPost.getLatitud());
mPost.setLongitud(mPost.getLongitud());
}
the MainActivity method: when the sendMethod is used i need to put a marker in the MainActivity map
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
UiSettings uiSettings = mMap.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
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) {
TastyToast.makeText(NavActivity.this, "GPS permission are not enabled", TastyToast.LENGTH_LONG, TastyToast.ERROR).show();
return;
}
mMap.setMyLocationEnabled(true);
}
my database methods:
// POST REFERENCE
public static DatabaseReference getPostRef() {
return FirebaseDatabase.getInstance()
.getReference("Pk");
}
public static Query getPostQuery() {
return getPostRef().orderByChild("Tc");
}
public static DatabaseReference getMyPostRef() {
return FirebaseDatabase.getInstance().getReference("Mp")
.child(getCurrentUser().getEmail().replace(".", ","));
}
回答1:
Firebase locationRef = mRootRef.child("your reference");
locationRef.addValueEventListener(new com.firebase.client.ValueEventListener() {
@Override
public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
for (DataSnapshot snapm: dataSnapshot.getChildren()) {
Double latitude = snapm.child("latitud").getValue(Double.class);
Double longitude = snapm.child("longitud").getValue(Double.class);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
throw firebaseError.toException();
}
});
来源:https://stackoverflow.com/questions/43701713/how-to-receive-latitude-and-longitude-data-stored-from-firebase-and-put-it-in-a