Reputation: 357
I'm trying to add markers to the map dynamically, getting the data from JSON, and that part is working, I can add multiple marker in multiple places, but when I click on the marker I want that a alert dialog opens, this alert dialog must contain some information regarding that marker, but right now the information I have is always the same, it's like if the the creation of the alert dialog is not in the same cycle that creates the marker.
See my code:
final JSONArray array = new JSONArray(jsonStrOportunidades);
for (int i = 0; i < array.length(); i++)
{
JSONObject jsonObject = array.getJSONObject(i);
final String Designacao = jsonObject.getString("Designacao");
String Coord_LAT = jsonObject.getString("Coord_LAT");
String Coord_LONG = jsonObject.getString("Coord_LONG");
final String Morada = jsonObject.getString("Morada");
final HashMap<String, String> oportunidades = new HashMap<>();
oportunidades.put("Designacao", Designacao);
oportunidades.put("Coord_LAT", Coord_LAT);
oportunidades.put("Coord_LONG", Coord_LONG);
oportunidades.put("Morada", Morada);
double lat1 = Double.parseDouble(Coord_LAT);
double lng1 = Double.parseDouble(Coord_LONG);
mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
new AlertDialog.Builder(MapsActivity.this)
.setTitle(Designacao)
.setMessage("Endereço: " + Morada + "\n" + "Telefone: " )
.setPositiveButton("Ir", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new GetDirecoes().execute();//Enviar as coordenadas
mBottomSheetBehavior.setPeekHeight(250);
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
return false;
}
});
listaOportunidades.add(oportunidades);
}
Upvotes: 0
Views: 1653
Reputation: 7271
Each time the method
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
new AlertDialog.Builder(MapsActivity.this)
.setTitle(Designacao)
.setMessage("Endereço: " + Morada + "\n" + "Telefone: " )
.setPositiveButton("Ir", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new GetDirecoes().execute();//Enviar as coordenadas
mBottomSheetBehavior.setPeekHeight(250);
}
})
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
return false;
}
});
is called, it is overriding the OnMarkerClickListener that you set previously. For this reason eventually the last OnMarkerClickListener is being set to map and that is why it is giving the same place information on every click (the information of the last place).
In order to solve the problem you must work with the marker object that is being received from the onMarkerClick(Marker marker) method and set the listener outside the loop. You may set the position as the tag to the marker and later in onMarkerClick(Marker marker) you can check the position and get related place info from listaOportunidades.
Marker m = mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));
m.setTag(i);
and later
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
int position = (int) marker.getTag();
// Show your alert dialog for the information related to the position
return false;
}
});
Upvotes: 1
Reputation: 3344
You are reassigning GoogleMap.OnMapClickListener
on the GoogleMap
object in every iteration of the loop. So, the listener will have the information of the last object in the JSON array. Make sure to only set OnMapClickListener
one time.
A solution for this would be to add your extra data to the marker and the extract that in the listener.
Marker marker = new MarkerOptions().position(new LatLng(lat1, lng1));
marker.setTag(extraInfo)
And then in the listener
public boolean onMarkerClick(Marker marker) {
ExtraInfo info = (ExtraInfo)marker.getTag();
}
Upvotes: 0