How do I select a marker using the Maps V2 Android API?

瘦欲@ 提交于 2019-11-30 21:10:36

Yes.

To determine which marker is selected, add a OnInfoWindowClickedListener to your GoogleMap:

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

Override the onInfoWindowClicked() method inside of the OnInfoWindowClickListener:

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

And keep track of the selected marker, perhaps with an instance variable.

To select a marker programmatically, you'll have to keep a list of all your markers, then get a handle on one and call showInfoWindow(), similar to this:

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

You can use the 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;
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!