I have the code to make one marker start a activity when you click on the infowindow. It works absolutely fine. But when I try to add in another marker and another @override
From MaciejGórski's help here is the example of having different classes(activities e.g pages) open when you click separate Marker infowindows on GoogleMapsV2:
add this in your GoogleMap class level:
private GoogleMap googlemap/mMap (or whatever you call yours);
private Map<Marker, Class> allMarkersMap = new HashMap<Marker, Class>();
Underneath protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); put in 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 above are the separate markers. If I was to create another one I would call it Marker marker3, then 4,5 ect... Where it asks for .class in allMarkersMap.put(marker, .class); input the class you want, so it opens what you want. Have the public void OnInfoWindowClick code underneath the markers somewhere, this is the callback.
And that's it. When you click on InfoWindows in markers they should open the activity class you have put in the MarkerOptions code!
Credit for this goes to MaciejGórski
The word set
in setOnInfoWindowClickListener
means it overrides any value that was set
before. This function is called on GoogleMap
object and because there is one GoogleMap
objects, there is one OnInfoWindowClickListener
that is active.
The way you work with it is decide what happens based on the parameter in callback onInfoWindowClick(Marker marker)
using if else
, switch
or maybe 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 earlier:
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);
}
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 already working in a fragment class (like happend to me) you will change:
public void onInfoWindowClick(Marker marker) {
Class<?> cls = allMarkersMap.get(marker);
Intent intent = new Intent(getActivity(), cls);
startActivity(intent);
}