我希望用户在Firebase数据库中添加和保存标记。 我不确定如何做到这一点,我目前让用户添加标记并保存它,但它不会出现在其他用户应用程序上。
public void onMapLongClick(final LatLng latLng) {
LayoutInflater li = LayoutInflater.from(context);
final View v = li.inflate(R.layout.alert_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(v);
builder.setCancelable(false);
builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText title = (EditText)v.findViewById(R.id.ettittle);
EditText snittle = (EditText)v.findViewById(R.id.etsnittle);
Marker marker0 = mMap.addMarker(new MarkerOptions()
.position(latLng)
.title(title.getText().toString()).snippet(snittle.getText().toString())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.tackshops)));
hash.put(marker0, R.drawable.agriculture);
prefs.edit().putString("Lat",String.valueOf(latLng.latitude)).apply();
prefs.edit().putString("Lng",String.valueOf(latLng.longitude)).apply();
locationCount++;
editor.putString("title"+(locationCount-1), title.getText().toString());
editor.putString("snippet"+(locationCount-1),snittle.getText().toString());
}
});
这是我在Firebase数据库中保存标记的方法
private void drawMarker(final LatLng latLng){
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
Agriculture marker = userSnapshot.getValue(Agriculture.class);
mMap.addMarker(new MarkerOptions().position(latLng))
.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.tackshops));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Failed to read value
}
});
答案 0 :(得分:0)
您应该在Firebase上保存您所在位置的坐标(Lat Long)。然后,您可以在地图上检索并显示标记。 Firebase Realtime Database这将告诉您如何保存和检索数据。
答案 1 :(得分:0)
将数据保存到FireBase中:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Sample_My_Location").push().setValue(l);
您可以在https://console.firebase.google.com/的FireBase控制台上将您的数据视为JSON结构。
确保您已在项目中成功设置了firebase。点击https://codelabs.developers.google.com/codelabs/firebase-android/#0
答案 2 :(得分:0)
MarkerOptions m2,m2;
Map<String, Object> objectMap = new HashMap<>();
objectMap.put("Marcador de partida", m1);
objectMap.put("Marcador de destino",m2);
//And to retrieve it
mDataBase.child("your_databade_child).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot p1)
{
if(p1.exists()){
String textRetrieved = p1.child("Marcador de destino/position/latitude").getValue().toString();
//Use textRetrieved as you please
}
else{
//Set error
}
// TODO: Implement this method
}
@Override
public void onCancelled(DatabaseError p1)
{
// TODO: Implement this method
}
});
}