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?

4

3 に答える 3

8

MaciejGórski のヘルプから、GoogleMapsV2 で別のマーカー情報ウィンドウをクリックしたときに、さまざまなクラス (ページなどのアクティビティ) を開く例を次に示します。

これを GoogleMap クラス レベルに追加します。

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

protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); の下 あなたのマーカーを入れてください:

    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);

}

上記は個別のマーカーです。別のものを作成する場合は、Marker マーカー 3、次に 4,5 ect... と呼びます。allMarkersMap.put(marker, .class); 必要なクラスを入力すると、必要なものが開きます。public void OnInfoWindowClick コードをマーカーの下のどこかに配置します。これがコールバックです。

以上です。マーカーで InfoWindows をクリックすると、MarkerOptions コードに入れたアクティビティ クラスが開きます。

これはMaciejGórskiの功績によるものです

于 2013-06-07T23:13:42.687 に答える