Android的谷歌地图V2 - OnInfoWindowClick几个标志物几个、地图、标志物、Android

2023-09-05 09:08:14 作者:我们的爱未完待续

我有code键使一个标志开始的活动,当你点击信息窗口。它的工作原理精绝。但是,当我试图在另一个标记添加和另一个@覆盖它总是会打开所有的标记信息窗口的最后一堂课。因此,在本质上,当点击,而不是打开单独的类,我打算给所有标记的​​信息窗口打开相同的活动。

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.

这是下面的code成功打开1活动上InfoWindowClicked。我把它称为example.class,这是每个人都需要这个例子的

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

所以,下面的的GoogleMap的GoogleMap / MMAP (或者无论你怎么称呼你),并在 @覆盖无效的onCreate (我的应用程序只启动,如果GooglePlayServices是可用的,你可能不像这样的),你可以把标记和infowindowclick code。

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.

确认某处的code有藏汉(通常是在一个私人无效setUpMap(){)

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

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

现在下面是code有两个标记,但他们都打开example2.class他们被点击时。有人可以帮助我弄清楚了这一点,这样我就可以把它们分开,并让他们打开不同的类别?

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


}

修改(2013年7月6日):的

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

}

以上^^^^是在我的保护无效的onCreate(包savedInstanceState){。有没有错误,当我调试,我可以看到标记,但不能点击信息窗口。该警告是:

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    

我看到这个警告两次的一流水平,一次在公共无效onInfoWindowClick上的字的的类的。我试着像类型参数添加到'类'一些不同的东西,但没有奏效。在标记标记在公共无效我改变了标记到< STRONG> MARKER1 和线下的 allMarkersMap.get(标记); 修改(标记)为(MARKER1)只是尝试,但没有奏效。有什么事我可以做的尝试和初始化onInfoWindowClick功能?

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?

推荐答案

从MaciejGórski的帮助,这里是具有不同类别(如活动页)打开,当你点击GoogleMapsV2单独标记信息窗口的例子:

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:

添加这在你的GoogleMap的一流水平:的

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

下方保护无效的的onCreate(包savedInstanceState){     super.onCreate(savedInstanceState); 把你的标记:

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

}

以上是单独标记。如果我是创造一个又一个我称之为标记marker3,然后4,5等...凡要求的.class中的 allMarkersMap.put(标记的.class); 输入类,你想要的,所以它会打开你想要什么。有标记下的公共无效OnInfoWindowClick code的地方,这是回调。

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.

就是这样。当您在标记点击信息窗口应该打开你放在MarkerOptions code中的活动课!

And that's it. When you click on InfoWindows in markers they should open the activity class you have put in the MarkerOptions code!

信用为这个去的MaciejGórski