1

This has been bugging me for a while now. For some reason, the markers from the firebase is not showing in the map. I have been searching for a while now and I almost looked into different answered questions here. Its either not applicable to my application, or it really doesn't work.

I tried to add static, hardcoded latlongs for markers. Added a bunch, and the app complies. It shows all the static, hardcoded markers. Unfortunately, together with the firebase call for loop code to create markers still doesn't show.

MapsActivity.java

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener, GoogleMap.OnMarkerClickListener, GoogleMap.OnMyLocationButtonClickListener, GoogleMap.OnMyLocationClickListener, GoogleMap.OnInfoWindowClickListener {

private GoogleMap mMap;
private UiSettings mUiSettings;

static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;

private DatabaseReference mBlog;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

Marker marker;
List<Blogzone> mapList;

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    initDrawer();

    mapList = new ArrayList<Marker>();
    mBlog = FirebaseDatabase.getInstance().getReference().child("Blogzone");
    mBlog.push().setValue(marker);


    //Firebase Auth
    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (mAuth.getCurrentUser() == null) {
                Intent loginIntent = new Intent(MapsActivity.this, LoginActivity.class);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(loginIntent);
            }
        }
    };
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mAuth.addAuthStateListener(mAuthListener);
    mMap = googleMap;
    mUiSettings = mMap.getUiSettings();


    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

        return;
    }

    mMap.setMyLocationEnabled(true);

    //  UI Settings state
    mUiSettings.setZoomControlsEnabled(true);
    mUiSettings.setMyLocationButtonEnabled(true);

    //Post Markers
    mMap.setOnMarkerClickListener(this);
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setOnInfoWindowClickListener(this);
    mMap.clear();

    final LatLng PH = new LatLng(16.566233,121.262634);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PH, 6));


    mBlog.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot s : dataSnapshot.getChildren()){

                Blogzone here = s.getValue(Blogzone.class);

                double latitude = Double.parseDouble(here.latitude);
                double longitude = Double.parseDouble(here.longitude);

                mapList.add(here);

                for (int i = 0; i < mapList.size(); i++)
                {
                    LatLng LOC = new LatLng(latitude,longitude);
                    if (mMap != null) {
                        Log.d("the locations", "Get location: " + latitude + ", " + longitude + " at " + here.title);
                        marker = mMap.addMarker(new MarkerOptions().position(LOC).title(here.title));
                    }
                }
            }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Blogzone.class

public class Blogzone {

public String title, desc, imageUrl, username;
public String latitude, longitude;

public Blogzone(String title, String desc, String imageUrl, String username, String latitude, String longitude) {
    this.title = title;
    this.desc = desc;
    this.imageUrl=imageUrl;
    this.username = username;
    this.latitude = latitude;
    this.longitude = longitude;
}

public Blogzone() {
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public void setUsername(String username) {
    this.username = username;
}

public void setTitle(String title) {
    this.title = title;
}

public void setDesc(String desc) { this.desc = desc; }

public void setlatitude(String latitude) { this.latitude = latitude; }

public void setLongitude(String longitude) { this.longitude = longitude; }

public String getUsername() {
    return username;
}

public String getImageUrl() {
    return imageUrl;
}

public String getTitle() {
    return title;
}

public String getDesc() {
    return desc;
}

public String getLatitude() {
    return latitude;
}
public String getLongitude() {
    return longitude;
}

}

edit: Here's the logs. there are some null string error but I think it doesn't affect the problem

Note that this is just a project app and I just learned from different tutorials and demos around the web. I just started coding Java last year.

I hope someone could help me soon.

Community
  • 1
  • 1
deewired
  • 11
  • 3

0 Answers0