1

this is my first question on the StackOverflow, thanks in advance. I have a question about the Android Google Map. Firstly, I get the data from the Firebase Cloud Firestore.

private void initMockData() {
    tags = new ArrayList<>();

    db = FirebaseFirestore.getInstance();
    db.collection("Tags").get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()) {
                    QuerySnapshot documents = task.getResult();
                    if(!documents.isEmpty()) {
                        List<DocumentSnapshot> list = documents.getDocuments();
                        for(DocumentSnapshot d : list) {
                            Tag tag = d.toObject(Tag.class);
                            tags.add(tag);
                        }
                    }
                }
            }
        });
}

Then in the OnMapReady(), I want to add markers on the map.

@Override
public void onMapReady(GoogleMap googleMap) {
    gMap = googleMap;
    gMap.setMyLocationEnabled(true);
    gMap.getUiSettings().setMyLocationButtonEnabled(false);
    gMap.clear();

    gMap.setOnMapClickListener(this);
    gMap.setOnMapLongClickListener(this);

    if(locationPermissionGranted) {
        startLocateUser();
    } else {
        askLocationPermission();
    }


    showTagsOnMap();
}

And showTagsOnMap():

private void showTagsOnMap() {
    for(Tag tag : tags) {
        LatLng latLng = new LatLng(tag.getLatitude(), tag.getLongtitude());
        gMap.addMarker(new MarkerOptions().position(latLng).title(tag.getTitle()));
    }
}

But those Markers do not show on the map. If I directly add marker on the map, it works just like below:

@Override
public void onMapReady(GoogleMap googleMap) {
    gMap = googleMap;
    gMap.setMyLocationEnabled(true);
    gMap.getUiSettings().setMyLocationButtonEnabled(false);
    gMap.clear();

    gMap.setOnMapClickListener(this);
    gMap.setOnMapLongClickListener(this);

    // this marker would show
    LatLng sydney = new LatLng(-33.852, 151.211);
    gMap.addMarker(new MarkerOptions().position(sydney).title("I am here"));

    if(locationPermissionGranted) {
        startLocateUser();
    } else {
        askLocationPermission();
    }
    // those markers do not show
    showTagsOnMap();

}

and when I then call the getLastLocation, those markers will show on the map.

private void fetchLastLocation() {
    try {
        if(locationPermissionGranted) {

            Task<Location> locationTask = client.getLastLocation();
            locationTask.addOnCompleteListener(this, new OnCompleteListener<Location>() {
                @Override
                public void onComplete(@NonNull Task<Location> task) {
                    if (task.isSuccessful()) {
                        userLocation = task.getResult();
                        if (userLocation != null) {
                            mapFragment = (SupportMapFragment) MapActivity.this.getSupportFragmentManager().findFragmentById(R.id.map);
                            mapFragment.getMapAsync(MapActivity.this);
                        } else {
                            Log.e(TAG, "user's location is null");
                        }
                    }
                }
            });
            locationTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e(TAG, "OnFailure" + e.getLocalizedMessage());
                }
            });
        } else {
            askLocationPermission();
        }
    } catch (SecurityException e) {
        Log.e("Exception: %s", e.getMessage());
    }
}

I am not very familiar with the Android google map, so I have no idea how to fix that.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
  • 2
    I think you have async problem. You can try when onMapReady then call initMockData and add marker after fetch data. – utkukutlu May 23 '20 at 17:20
  • Firebase API is asynchronous, @UtkuKUTLU provided a solution you should go ahead with it. You can also take a look at my answer from the following **[post](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)**. – Alex Mamo May 24 '20 at 13:38
  • 1
    @AlexMamo Thank you very much for your suggestion and I worked it out with the youtube link you provided – Will Jarvin May 25 '20 at 03:13
  • and Thank you for your advice @UtkuKUTLU although I could not understand your advice at first. – Will Jarvin May 25 '20 at 03:13
  • And @MrBean Bremen Thank you for your help editing my question's format. – Will Jarvin May 25 '20 at 03:31

0 Answers0