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.