Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am currently using the ItemizedOverlay class from the Maps V1 API, which keeps track of what marker (if any) is currently selected. Is there any similar functionality in Maps V2 to determine which marker is currently selected? Also, is there a way to programatically select a new marker?

like image 215
noisecapella Avatar asked Jan 14 '13 03:01

noisecapella


People also ask

How do I drag a marker on Google Maps Android?

Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.

How do I get a marker position on Google Maps?

You can add a simple marker to the map at a desired location by instantiating the marker class and specifying the position to be marked using latlng, as shown below.

How do you find the map marker?

It is obtainable from the president at The Pokétch Company in Jubilife City when you got yourself three badges (Cobble Badge for Pokémon Diamond/Pearl, Relic Badge for Pokémon Platinum.)


2 Answers

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();
}
like image 72
DiscDev Avatar answered Sep 22 '22 06:09

DiscDev


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;
        }
    });
like image 36
chris Avatar answered Sep 22 '22 06:09

chris