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

在受保护的 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 marker3,然后是 4,5 等......它在allMarkersMap.put(marker, .class);中要求 .class 输入你想要的类,所以它会打开你想要的。在某处的标记下方有 public void OnInfoWindowClick 代码,这是回调。

就是这样。当您在标记中单击 InfoWindows 时,它们应该会打开您在 MarkerOptions 代码中放置的活动类!

这要归功于 MaciejGórski

于 2013-06-07T23:13:42.687 回答
1

setin这个词setOnInfoWindowClickListener意味着它覆盖了set之前的任何值。此函数在GoogleMap对象上调用,因为有一个GoogleMap对象,所以有一个OnInfoWindowClickListener是活动的。

您使用它的方式是根据回调中的参数决定发生onInfoWindowClick(Marker marker)什么if elseswitch或者Map<Marker, Class>

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

当然,您需要更早地初始化此映射:

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

编辑:

// 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);
}
于 2013-06-02T17:54:43.453 回答
0

对于问题:

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

我添加< ? >在班级旁边:

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

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

如果你已经在一个片段类中工作(就像发生在我身上)你会改变:

public void onInfoWindowClick(Marker marker) {
    Class<?> cls = allMarkersMap.get(marker);
    Intent intent = new Intent(getActivity(), cls);
    startActivity(intent);
}
于 2014-06-09T04:54:25.050 回答