简体   繁体   English

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

[英]How to retrieve data from Firebase to make multiple Markers on map Android

this is my firebase look like 这是我的基地

firebase . 火力基地

And this my maps activity : 这是我的地图活动:

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) {

            }
        });

    }
}

and the error says 错误说

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

and when i run it, it doesn't show the marker and the location still get wrong. 当我运行它时,它不显示标记,并且位置仍然出错。 I think i name it wrong about the "child" and get value in it, but i have no idea how to figure it out. 我认为我对“孩子”的说法不对,并从中获得了价值,但我不知道如何弄清楚。

(the url link is all right) (网址链接可以)

To solve this, please change the following line of code: 要解决此问题,请更改以下代码行:

firebase = new Firebase(FIREBASE_URL);

to

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

You are missing a child from the tree, which is marker and which is mandatory be used in your reference as well. 您从树中丢失了一个孩子,该孩子是marker ,也必须在参考中使用。

Further more, remove the .child("marker") from the followig line of code: 此外,从以下代码行中删除.child("marker")

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

Shoul only be: 应该仅是:

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

Also note, that you are using a very old version of Firebase and I recommend you update to the latest one. 另请注意,您使用的是Firebase的旧版本,建议您更新到最新版本。

I have tried with this one 我已经尝试过了

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) {

            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有多个节点的 firebase 中检索数据? - How retrieve data from firebase with multiple nodes? 如何从Firebase数据库检索多个数据? - How to retrieve multiple data from Firebase database? 如何使用 Map 函数从 firebase 检索多张图片 - How to retrieve multiple picture from firebase using Map function 如何从Firebase数据库到Android地图应用程序检索纬度和经度 - How to retrieve latitude and longitude from firebase database to android map application 如何从 Firestore 检索多个用户名并将用户名分配给 map 上的正确标记 - How to retrieve multiple username from Firestore and assign the username to the correct markers on map 如何从Firebase数据库检索数据以显示标记? - How can I retrieve data from my Firebase Database to display markers? 从 Firebase 检索多个数据到我的 Android 应用程序 - Retrieve multiple data from Firebase to my Android applciation 如何从android的firebase数据库中的uid获取/检索多个数据? - How do I get/retrieve multiple data from uid in the firebase database in android? 如何从sqlite在地图中显示多个标记 - how to show multiple markers in map from sqlite 如何在Android中制作SVG地图? (并在地图上放置标记) - How to make a SVG map in android? (and place markers on the map)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM