I am new to the android studio.I already know that there are multiple questions related with my issue in javascript. But what I want is to display multiple info windows for the locations, retreived from firestore.I have already displayed multiple locations from firestore.But when I click any marker on the google map, the info window is always displayed only for the last location detail in the firestore. This is my code for InfoWindowAdapter:
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
private Context context;
public MarkerInfoWindowAdapter(Context context) {
this.context = context.getApplicationContext();
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.map_marker_info_window, null);
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
LatLng latLng = marker.getPosition();
TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);
tvLat.setText("Latitude:" + latLng.latitude);
tvLng.setText("Longitude:"+ latLng.longitude);
return v;
}
}
This is my code for MainActivity :
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 23;
private GoogleMap mMap;
Button btn;
private DatabaseReference collectionRef;
private GeoFirestore geoFirestore;
private ChildEventListener mChildEventListener;
Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
CollectionReference collectionRef = FirebaseFirestore.getInstance().collection("my-collection");
GeoFirestore geoFirestore = new GeoFirestore(collectionRef);
ChildEventListener mChildEventListener;
// collectionRef.push().setValue(marker);
}
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
} else {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap = googleMap;
FirebaseFirestore mDatabase = FirebaseFirestore.getInstance();
CollectionReference mOrderRef = mDatabase.collection("my-collection");
Task<QuerySnapshot> querySnapshotTask = mOrderRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
if (documentSnapshot.contains("latitude") && documentSnapshot.contains("longitude")) {
String lat = (String) documentSnapshot.get("latitude");
String lon = (String) documentSnapshot.get("longitude");
String Place = (String) documentSnapshot.get("place");
String Reason = (String) documentSnapshot.get("reason");
if (lat != null && lon != null && !lat.isEmpty() && !lon.isEmpty()) {
double latitude = Double.parseDouble(lat.trim());
double longitude = Double.parseDouble(lon.trim());
final LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng).title(Place));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
MarkerInfoWindowAdapter markerInfoWindowAdapter = new MarkerInfoWindowAdapter(getApplicationContext());
googleMap.setInfoWindowAdapter(markerInfoWindowAdapter);
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// mMap.clear();
MarkerOptions markerOptions = new MarkerOptions();
// MarkerOptions markerOptions1= new MarkerOptions();
markerOptions.position(latLng);
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
Marker marker1 = mMap.addMarker(markerOptions);
// Marker marker2 = mMap.addMarker(markerOptions1);
marker1.showInfoWindow();
return true;
}
});
// mMap.addMarker(new MarkerOptions().position(latLng).title(Place));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
}
}
});
}
}
Can someone help me to fix my issue?