不太了解从Firebase数据库检索标记位置 [英] Not really understanding retrieving marker location from Firebase Database

查看:66
本文介绍了不太了解从Firebase数据库检索标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找答案并尝试了许多教程,但仍然无法解决问题.我正在开发一个应用程序,用户可以在地图上添加标记,其他所有用户都可以看到标记.但是我无法使其工作,因为数据库中的标记永远不会显示.

I looked for an answer and tried a lot of tutorials but i still can't get it right.I'm working on an app in which user can add marker to the map, and all the other users will see the marker. But i can't make it work because the markers from database never show.

这是我从数据库中读取的代码:

this is my code for reading from database:

mLocation.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                MarkerLocation markerLocation = dataSnapshot.child("location").getValue(MarkerLocation.class);
                for (DataSnapshot s : dataSnapshot.getChildren()){
                double latitude = (double) (s.child("lat").getValue());
                double longitude = (double) ((s.child("lng").getValue()));
                LatLng location = new LatLng(latitude,longitude);
                mMap.addMarker(new MarkerOptions().position(location));
                }
            }

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

            }
        });

和firebase数据库如下:

and firebase database look like this:

  • 应用

  • application

  -location
        -marker
             -latitude:43.849
             -longitude:-109.737

任何有帮助的人.

推荐答案

要根据您的实际架构在地图上添加数据库中所有标记,请使用以下代码行:

To add all the markers from your database on the map according to your actual schema, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference locationRef = rootRef.child("location");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            MarkerLocation markerLocation = ds.getValue(MarkerLocation.class);
            LatLng latLng = new LatLng(markerLocation.getLatitude(), markerLocation.getLongitude());
            mMap.addMarker(new MarkerOptions().position(latLng));
            Log.d(TAG, markerLocation.getLatitude() + ", " +  markerLocation.getLongitude());
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
locationRef.addListenerForSingleValueEvent(valueEventListener);

如果直接将值设置到公共字段,请更改以下代码行:

If you are setting the values directly onto public fields, please change the following line of code:

LatLng latLng = new LatLng(markerLocation.getLatitude(), markerLocation.getLongitude());

LatLng latLng = new LatLng(markerLocation.latitude, markerLocation.longitude);

这篇关于不太了解从Firebase数据库检索标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆