如何从Firebase检索数据以在地图Android上进行多个标记

时间:2019-01-11 09:20:35

标签: java android firebase google-maps

这是我的火力库外观

firebase

这是我的地图活动:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
    private GoogleMap mMap;
    private DatabaseReference mUsers;
    Marker marker;
    public static final String FIREBASE_URL = "https://xxx.firebaseio.com/";
    private Firebase firebase;
    @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.
        Firebase.setAndroidContext(this);
        firebase = new Firebase(FIREBASE_URL);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        firebase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) {
                for (com.firebase.client.DataSnapshot s : dataSnapshot.child("marker").getChildren()) {
                    String lat = s.child("lat").getValue().toString();
                    String lng = s.child("lng").getValue().toString();

                    double latitude = Double.parseDouble(lat);
                    double longitude = Double.parseDouble(lng);
                    LatLng loc = new LatLng(latitude, longitude);
                    googleMap.addMarker(new MarkerOptions().position(loc).title(s.child("nama").getValue().toString()));
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

    }
}

错误提示

E/MarketPageConfiguration: parse page configuration json error
    org.json.JSONException: No value for homeIndex

当我运行它时,它不显示标记,并且位置仍然出错。我认为我对“孩子”的说法不对,并从中获得了价值,但我不知道如何弄清楚。

(URL链接正常)

2 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

firebase = new Firebase(FIREBASE_URL);

firebase = new Firebase(FIREBASE_URL).child("marker");

您从树中缺少一个孩子,marker,并且在参考中也必须使用。

此外,从以下代码行中删除.child("marker")

for (com.firebase.client.DataSnapshot s : dataSnapshot.child("marker").getChildren())

只能是:

for (com.firebase.client.DataSnapshot s : dataSnapshot.getChildren())

还请注意,您使用的是Firebase的旧版本,建议您更新到最新版本。

答案 1 :(得分:0)

我已经尝试过这个

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
//    public static final String FIREBASE_URL = "https://trans-k-1546744104547.firebaseio.com/";
    private FirebaseDatabase firebase;
    private DatabaseReference databaseReference;

    @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.
        firebase = FirebaseDatabase.getInstance();
        databaseReference = firebase.getReference("marker");

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    String lat = child.child("lat").getValue().toString();
                    String lng = child.child("lng").getValue().toString();

                    double latitude = Double.parseDouble(lat);
                    double longitude = Double.parseDouble(lng);
                    LatLng loc = new LatLng(latitude, longitude);
                    googleMap.addMarker(new MarkerOptions().position(loc).title(child.child("nama").getValue().toString()));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}