AndreasK AndreasK - 3 months ago 32
Android Question

Android Google Maps, multiple intents

I have some google maps markers (10) with info window. By clicking on an info window a new activity should start and a value should be transfered via intent.put extra():
My problem is that always the value of the last marker is transfered to the next activity, although i click on another info window, in this case it's 101010.
My Code:

Marker marker1 = map.addMarker(new MarkerOptions()
.title(itemList.get(1))
.icon(BitmapDescriptorFactory.fromResource(getDrawableId(imagename)))
.position(new LatLng(Double.parseDouble(itemList.get(2)), Double.parseDouble(itemList.get(3))))

);



System.out.println("MoID=" + monsterid1);
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker1) {
Intent intent1 = new Intent(showmap.this, MonsterDetail.class);
intent1.putExtra("monsterid", monsterid); //value i want to transfer to next activity e.g "111"
startActivity(intent1);


}
});


Marker marker2 = map.addMarker(new MarkerOptions()
.title(itemList.get(5))
.icon(BitmapDescriptorFactory.fromResource(getDrawableId(imagename)))
.position(new LatLng(Double.parseDouble(itemList.get(6)), Double.parseDouble(itemList.get(7))))

);



System.out.println("MoID=" + monsterid1);
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker2) {
Intent intent2 = new Intent(showmap.this, MonsterDetail.class);
intent2.putExtra("monsterid", monsterid2); //value i want to transfer to next activity e.g. "222"
startActivity(intent2);


}
});

Marker marker10 = map.addMarker(new MarkerOptions()
...
...
...
intent10.putExtra("monsterid", monsterid10); //value i want to transfer to next activity e.g. "101010"

Answer

The OnInfoWindowClickListener is for the map, not the marker. So, your current code is:

  • Creating an OnInfoWindowClickListener

  • Creating another OnInfoWindowClickListener, throwing away the previous one

  • and so on

Call setOnInfoWindowClickListener() once. In onInfoWindowClick(), you are passed a Marker that represents what the user clicked on. Using that Marker, determine the value of the extra to put into your Intent. For example, IIRC, Marker has a getId() method, so you could have a HashMap<String, String> mapping marker IDs to extra values, where in onInfoWindowClick() you look up the value for the clicked-upon marker.

Comments