如何在标记

时间:2016-02-02 09:36:57

标签: android arraylist firebase google-maps-markers

在Android中我通过以下代码获取firebase response(latt,lngg)

ref.child("USerCurrentLocation").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        size = (int) dataSnapshot.getChildrenCount();
        lattDonor = new double[size];
        lnggDonor = new double[size];
        int i = 0;
        Log.d("size1"," size = "+size);
        Log.d("latt",dataSnapshot.getKey()+" , "+dataSnapshot.getValue());
        for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
            Double latt = (Double) messageSnapshot.child("latt").getValue();
            Double lngg = (Double) messageSnapshot.child("lngg").getValue();
            Log.d("latt1", latt + " , " + lngg);
            Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(latt,lngg)).title("Donor"));
            markers.add(marker);
            }
        }

现在我在Log中得到了这个结果:

02-02 14:26:50.175 4032-4032/com.example.haawztech.abdapplication D/latt: USerCurrentLocation , {2233={lngg=45.34534234, latt=56.44332453}, 1234={lngg=67.0833, latt=46.45398745}, 410060215718654={lngg=67.0555454, latt=24.8731362}, 2341={lngg=67.0333, latt=24.9167}, 1345={lngg=53.09865348, latt=34.45739834}}
02-02 14:26:50.185 4032-4032/com.example.haawztech.abdapplication D/latt1: 46.45398745 , 67.0833
02-02 14:26:50.185 4032-4032/com.example.haawztech.abdapplication D/latt1: 34.45739834 , 53.09865348
02-02 14:26:50.185 4032-4032/com.example.haawztech.abdapplication D/latt1: 56.44332453 , 45.34534234
02-02 14:26:50.195 4032-4032/com.example.haawztech.abdapplication D/latt1: 24.9167 , 67.0333
02-02 14:26:50.195 4032-4032/com.example.haawztech.abdapplication D/latt1: 24.8731362 , 67.0555454

但是当我在标记中设置这些值时,我刚刚使用此latt子项中的最后lngg firebase标记。

如何在此循环中获取此项目中的所有值标记?

我的firebase数据集ID

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要创建一个Marker数组,用于保存所有标记。使用变量'size'初始化数组

    Marker[] allMarkers = new Marker[size];

之后在DataSnapshot for..loop中引入for..loop。最后使用之前创建的数组对象在地图上创建标记。以下是如何实现它的完整代码。

    ref.child("USerCurrentLocation").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    size = (int) dataSnapshot.getChildrenCount();

    lattDonor = new double[size];
    lnggDonor = new double[size];
    int i = 0;
    Log.d("size1"," size = "+size);
    Log.d("latt",dataSnapshot.getKey()+" , "+dataSnapshot.getValue());

    Marker[] allMarkers = new Marker[size];


    for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {

        for(int i=0;i<=size;i++) {

        try {

        Double latt = (Double) messageSnapshot.child("latt").getValue();
        Double lngg = (Double) messageSnapshot.child("lngg").getValue();
        Log.d("latt1", latt + " , " + lngg);

        allMarkers[i] = mMap.addMarker(new MarkerOptions().position(new LatLng(latt,lngg)).title("Donor"));

       // markers.add(marker);
        } catch(Exception ex){}
        }

    }