Android Google Maps V2 - OnInfoWindowClick on multiple markers

I have a code so that a single marker starts an action when you click on infowindow. It works absolutely fine. But when I try to add another @override to another marker, it always opens the last class in all Markers windows. Thus, in fact, all infowindows tokens open the same activity when pressed, and do not open the separate class that I intended to do this.

This is the code below that successfully opens 1 activity in InfoWindowClicked. I called it example.class, this is for everyone who needs this example.

public class MainActivity extends Activity implements OnInfoWindowClickListener { private GoogleMap googlemap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(isGooglePlay()){ setContentView(R.layout.activity_main); setUpMap(); { } } googlemap.addMarker(new MarkerOptions() .position(new LatLng(0,-0)) .title("Title") .snippet("Snippet") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { @Override public void onInfoWindowClick(Marker marker) { Intent intent = new Intent(MainActivity.this,Example.class); startActivity(intent); } }); { 

So, under googlemap googlemap / mMap (or whatever you call) and @override void Oncreate (my application only starts if the GooglePlayServices application is available like this) you can put the marker and code infowindowclick.

Make sure that somewhere in the code there is also (usually in private void setUpMap () {)

  googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap(); 

Now below is the code with two markers, but both of them open example2.class when clicked. Can someone help me figure this out so that I can separate them and open different classes for them?

  googlemap.addMarker(new MarkerOptions() .position(new LatLng(0,-0)) .title("Title") .snippet("Snippet") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { @Override public void onInfoWindowClick(Marker marker) { Intent intent = new Intent(MainActivity.this,Example.class); startActivity(intent); } }); { { googlemap.addMarker(new MarkerOptions() .position(new LatLng( 0, -0)) .title("Title") .snippet("Snippet") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { @Override public void onInfoWindowClick(Marker marker) { Intent intent = new Intent(MainActivity.this,Example2.class); startActivity(intent); } }); }} } 

Edit (07/06/2013):

 private GoogleMap googlemap; private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>(); 

The above applies to class level ^^^

  Marker marker1 = googlemap.addMarker(new MarkerOptions() .position(new LatLng(0,0)) .title("England") .snippet("London") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); allMarkersMap.put(marker1, Contact.class); } public void onInfoWindowClick(Marker marker) { Class cls = allMarkersMap.get(marker); Intent intent = new Intent(MainActivity.this, cls); startActivity(intent); } 

The above ^^^^ is under my "protected void onCreate (Bundle savedInstanceState) {". There are no errors when I debug, I see a marker, but I can not click on InfoWindow. Warnings:

 Class is a raw type. References to generic type Class<T> should be parameterized 

I see this warning twice at the class level and once in public void onInfoWindowClick on the word "Class" . I tried several different things, such as “Add argument types to“ Class ”, but that didn’t work. In Marker-Marker in a public void, I changed the marker on marker1 and on the line below allMarkersMap.get (marker); changed (marker) to (marker1) just for a try, but that didn't work. Is there anything else I can do to try to initialize the onInfoWindowClick function?

+4
source share
3 answers

From MaciejGórski's help, here is an example of how different classes (actions, for example, pages) open when you click separate Marker info windows on GoogleMapsV2:

add this at your level of googlemap:

 private GoogleMap googlemap/mMap (or whatever you call yours); private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>(); 

Protected by void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); enter your markers:

  Marker marker = googlemap.addMarker(new MarkerOptions() .position(new LatLng(0,-0)) .title("London") .snippet("North London") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); allMarkersMap.put(marker, NorthLondon.class); googlemap.setOnInfoWindowClickListener(this); Marker marker1 = googlemap.addMarker(new MarkerOptions() .position(new LatLng(0244534,-1232312)) .title("USA") .snippet("Washington") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); allMarkersMap.put(marker1, Washington.class); googlemap.setOnInfoWindowClickListener(this); Marker marker2 = googlemap.addMarker(new MarkerOptions() .position(new LatLng(42343244,-0.334322)) .title("Italy") .snippet("Rome")); allMarkersMap.put(marker2, Rome.class); googlemap.setOnInfoWindowClickListener(this); } public void onInfoWindowClick(Marker marker) { Class cls = allMarkersMap.get(marker); Intent intent = new Intent(MainActivity.this, cls); startActivity(intent); } 

The individual markers are indicated above. If I were to create another one, I would call it marker-marker3, then 4.5 equiv ... Where does it request .class in allMarkersMap.put (marker, .class); enter the class you want, so it opens what you want. Highlight the OnInfoWindowClick public code under the markers somewhere, this is a callback.

What is it. When you click on InfoWindows in markers, they should open the activity class that you put in the MarkerOptions code!

Credit for this - MaciejGórski

+7
source

The word set in setOnInfoWindowClickListener means that it overrides any value that was set before. This function is called in the GoogleMap object, and since there is one GoogleMap object, one OnInfoWindowClickListener active.

The way to work with it is to decide what happens based on the onInfoWindowClick(Marker marker) callback parameter, using if else , switch or possibly Map<Marker, Class> :

 public void onInfoWindowClick(Marker marker) { Class cls = map.get(marker); Intent intent = new Intent(MainActivity.this, cls); startActivity(intent); } 

Of course, you need to initialize this map first:

 Marker marker1 = googlemap.addMarker... map.put(marker1, Example.class); 

Edit:

 // on the class level: private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>(); // in the onCreate or elsewhere Marker marker1 = googlemap.addMarker(new MarkerOptions() .position(new LatLng(0,-0)) .title("Netherlands") .snippet("Amsterdam") .icon(BitmapDescriptorFactory.fromResource(R.drawable.star))); allMarkersMap.put(marker1, Example.class); // callback public void onInfoWindowClick(Marker marker) { Class cls = allMarkersMap.get(marker); Intent intent = new Intent(MainActivity.this, cls); startActivity(intent); } 
+1
source

For the problem:

 Class is a raw type. References to generic type Class<T> should be parameterized 

i add <? > next to class:

 private Map<Marker, Class<?>> allMarkersMap = new HashMap<Marker, Class<?>>(); 

and

 Class<?> cls = allMarkersMap.get(marker); 

And if you are already working in a fragment class (for example, with me about me), you will change:

 public void onInfoWindowClick(Marker marker) { Class<?> cls = allMarkersMap.get(marker); Intent intent = new Intent(getActivity(), cls); startActivity(intent); } 
0
source

Source: https://habr.com/ru/post/1484061/