5

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 it always opens the last class on all of the Markers infowindows. So in essence all of the markers infowindows open the same activity when clicked instead of opening the separate class that I intended it to.

This is the code below that successfully opens 1 activity on InfoWindowClicked. I have called it example.class, this is for everyone that 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 underneath GoogleMap googlemap/mMap (or whatever you call yours) and the @override void Oncreate (my application only starts if GooglePlayServices is available, yours might not be like this) you can put the marker and infowindowclick code.

Make sure somewhere in the code there is aswell (usually in a private void setUpMap(){ )

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

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

 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 is on the 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 can see the Marker but cannot click on the InfoWindow. The warnings are:

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

I see this warning twice on the class level and once in the public void onInfoWindowClick on the word 'Class'. I've tried a few different things like "Add type arguments to 'Class' but it didn't work. On Marker marker in the public void I changed marker to marker1 and on the line below allMarkersMap.get(marker); changed (marker) to (marker1) just to try but it didn't work. Is there anything else I can do to try and initialize the onInfoWindowClick function?

user1977908
  • 1,105
  • 4
  • 11
  • 16
  • Have you registered `OnInfoWindowClickListener` like in the original code? – MaciejGórski Jun 07 '13 at 17:32
  • Yes I have. I've put in the OnInfoWindowClickListener in the 'MainActivity extends Activity implements' at the top. I've tried the public void onInfoWindowClick(Marker marker) with and without @Override above it. I don't know why its not registering. I've taken out the googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { like in the original. – user1977908 Jun 07 '13 at 17:47
  • And calling `googlemap.setOnInfoWindowClickListener(this);`? – MaciejGórski Jun 07 '13 at 17:50
  • Okay its working! Let me just try adding a few more in! – user1977908 Jun 07 '13 at 17:52
  • It worked for 1 marker, when I tried to add in two markers my map wouldn't actually load anymore. I'm getting AndroidRuntime errors including .handleLaunchActivity and IBitmapDescriptorFactory is not initialized. FATAL EXCEPTION: main. – user1977908 Jun 07 '13 at 18:16
  • I'll try a few different things later but it should work soon – user1977908 Jun 07 '13 at 18:23
  • I've figured it out from your help, it works perfectly! I will put up example code! – user1977908 Jun 07 '13 at 22:50

3 Answers3

8

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

Jack
  • 3,632
  • 7
  • 45
  • 67
user1977908
  • 1,105
  • 4
  • 11
  • 16
1

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);
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Thanks for your help MaciejGórski!, I have tried to implement the code that you gave me and I had a few errors, could you have a look at the edit I've made to the question above and over-see the code I've changed. If you get a minute please help me out – user1977908 Jun 05 '13 at 20:30
  • @user1977908 `map` in my code is not of `GoogleMap` class, but rather `Map` or `HashMap`. You need to create it and put all markers there when creating them. – MaciejGórski Jun 05 '13 at 20:35
  • Hi MaciejGorski it keeps saying Map cannot be resolved. Please could you show me an example, that would really help – user1977908 Jun 05 '13 at 20:50
  • Thanks again for your help MaciejGorski, you are a ledge! I've implemented the code and there are no errors, only a few warnings. But when I click on the infowindow nothing happens. It's literally the home stretch now, we are so close! i'm going to edit my question with the code, please have a final look when you can – user1977908 Jun 07 '13 at 17:26
0

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);
}
TheX_H
  • 105
  • 1
  • 1
  • 11