使用Firebase实时数据库在地图上显示标记

时间:2017-01-17 20:22:21

标签: android google-maps firebase firebase-realtime-database google-maps-markers

我正在开发一个Android应用程序,其中我使用Firebase作为后端进行身份验证和存储数据。

我可以从Firebase数据库存储和检索位置,但无法在地图上显示标记。

代码如下:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
{


    protected void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_maps);

        //Retrieve data from the firebase

    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference myRef = database.getReference().child("users");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            collectLocation((Map<String, Object>) dataSnapshot.getValue());

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
      });
     }

private void collectLocation(Map<String, Object> users)
    {
        for (Map.Entry<String, Object> entry : users.entrySet())
        {
            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            locationData.add((String) singleUser.get("location"));

            count[0]++;

        }

        System.out.println("Location: " + locationData.toString());
        System.out.println("My count is: " + count[0]);


        for(String loc : locationData)
        {
            System.out.println("LocationMMdm" + loc);
            String[] Location = loc.split(",");

            for(int i=0;i<2;i++)
            {
                System.out.println("LOcation " + i + " " + Location[i]);
                if (i==0) {
                    lat = Double.parseDouble(Location[i]);
                }
                if (i==1){
                    log = Double.parseDouble(Location[i]);
                }
            }
            System.out.println("lat: " + lat + " " + "log: " + log);
            //mMap.addMarker(new MarkerOptions().position(new LatLng(loc.)).title("A"));

        }
    }

    private void collectdriverInfo(Map<String, Object> users)
    {

        for (Map.Entry<String, Object> entry : users.entrySet())
        {
            //Get user map
            Map singleUser = (Map) entry.getValue();
            //Get phone field and append to list
            DriverInfo.add((String) singleUser.get("driverInfo"));
        }
        System.out.println("Driver Information: " + DriverInfo.toString());
    }

@Override
public void onMapReady(GoogleMap googleMap)
  {
    mMap = googleMap;
    mMap.setMyLocationEnabled(true);
    mMap.addMarker(new MarkerOptions().position(new LatLng(lat,log)).title("A"));

   }

}

我已将全局声明的latlog中的纬度和经度值存储起来。但我无法从onMapReady函数

访问它们

0 个答案:

没有答案