如何使用Maps V2 Android API选择标记?

时间:2013-01-14 03:18:22

标签: android google-maps-android-api-2

我目前正在使用Maps V1 API中的ItemizedOverlay类,该类会跟踪当前选择的标记(如果有)。 Maps V2中是否有类似的功能来确定当前选择的是哪个标记?另外,有没有办法以编程方式选择新标记?

2 个答案:

答案 0 :(得分:16)

要确定选择了哪个标记,请将OnInfoWindowClickedListener添加到GoogleMap中:

//mMap is an instance of GoogleMap
mMap.setOnInfoWindowClickListener(getInfoWindowClickListener());

覆盖OnInfoWindowClickListener中的onInfoWindowClicked()方法:

public OnInfoWindowClickListener getInfoWindowClickListener()
{
    return new OnInfoWindowClickListener() 
    {       
        @Override
        public void onInfoWindowClick(Marker marker) 
        {
            Toast.makeText(getApplicationContext(), "Clicked a window with title..." + marker.getTitle(), Toast.LENGTH_SHORT).show();
        }
    };      
}

跟踪选定的标记,可能使用实例变量。

要以编程方式选择标记,您必须保留所有标记的列表,然后获取一个标记并调用showInfoWindow(),类似于:

//markerList is just a list keeping track of all the markers you've added
//to the map so far, which means you'll have to add each marker to this
//list as you put it on the map
Marker marker = this.markerList.get(someObjectYoureShowingAMarkerFor.getId());

if(marker != null)
{
    marker.showInfoWindow();
}

答案 1 :(得分:1)

您可以使用OnMarkerClickListener。

googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean  onMarkerClick(Marker marker) {
            Toast.makeText(getApplicationContext(), "Clicked a marker with title..." + marker.getTitle(), Toast.LENGTH_SHORT).show();
            return true;
        }
    });