我需要在地图中为Firebase数据库中的每个用户添加标记

时间:2019-01-30 18:33:14

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

我能够在地图上为我的应用中已登录用户动态显示标记,但是我需要为在Firebase DB中注册的所有用户显示标记。

我尝试使用以下代码获取所有数据:

公共类LentiMapsActivity扩展FragmentActivity实现OnMapReadyCallback {

private GoogleMap mMap;

private FirebaseAuth mAuth;
private DatabaseReference UserRef;

private static final String TAG = LentiMapsActivity.class.getSimpleName();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lenti_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Intent intent = new Intent(LentiMapsActivity.this, TrackerActivity.class);
    startActivity(intent);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if(lm != null){
        if(location != null){
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
        == PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        == PackageManager.PERMISSION_GRANTED) {
    mMap.setMyLocationEnabled(true);
}
    mMap.setTrafficEnabled(false);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 11));
    mMap.setMaxZoomPreference(16);

    loadMarker();
}

private void loadMarker() {
    final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    rootRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                String key = dataSnapshot1.getKey();
                localList.add(key);

                Local localCurrent = dataSnapshot.child(uid).child("latlong").getValue(Local.class);
                LatLng latLngCurrent = new LatLng(localCurrent.getLatitude(),localCurrent.getLongitude());

                for (int i = 0; i<localList.size(); i++)
                {
                    Local local = dataSnapshot.child(key).child("latlong").getValue(Local.class);
                    Local nick = dataSnapshot.child(key).getValue(Local.class);
                    if (local != null){
                        LatLng latLng = new LatLng(local.getLatitude(),local.getLongitude());
                        if (mMap != null)
                        {
                            mMap.addMarker(new MarkerOptions().position(latLng).title(nick.getNickname()));
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLngCurrent,11));
                        }
                    }
                }
            }
        }
   }

代码没有错误,标记在地图上动态显示,但是我无法为所有注册用户在地图上填写标记

0 个答案:

没有答案