user2843350
user2843350

Reputation:

How to specify onMarkerclick() for different types of markers in android

On google map i am placing different colors of markers for different purposes, here i want different functionality for onMarkerclick() for each marker( ex:all green color markers).How to make condition for this.here is my code to create one set of markers

@Override
public void onMapLongClick(LatLng touchlocation) {
    // TODO Auto-generated method stub
    googleMap.addMarker(new MarkerOptions()
            .position(touchlocation)
            .title("Event")
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}

Here another type of marker

// adding marker
        user_marker = googleMap.addMarker(new MarkerOptions()
                .position(USER_LOCATION)
                .title("User")
                .snippet("User registered Location:" + loc)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));

For nearby places i am using another type of marker

here how i implemented onMarkerClick()

@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub
    // googleMap.clear();
    Toast.makeText(getApplicationContext(), "USER MARKER",
            Toast.LENGTH_LONG).show();
    showDialog(DIALOG_LOGIN);

    return true;
}

here i want different functionality for each type of marker ,how to write a condition for that.help me.

Upvotes: 4

Views: 10131

Answers (3)

Braian Coronel
Braian Coronel

Reputation: 22867

I create a listener for each marker. This way I will have a more organized and maintainable code.

private GoogleMap.OnMarkerClickListener portoAlegreMarkerClickListener = new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        Toast.makeText(getApplicationContext(), "Welcome to Porto Alegre", Toast.LENGTH_SHORT).show();
        return false;
    }
};

Then I created the marker and in the following instruction I passed the private listener as a tag. This worked. If someone knows how to pass this listening in a better way then share it with us.

@Override
public void onMapReady(GoogleMap googleMap) {    
    markerPortoAlegre = googleMap.addMarker(new MarkerOptions()
            .position(portoAlegre)
            .title("Porto Alegre"));
    markerPortoAlegre.setTag(portoAlegreMarkerClickListener);
}

Finally I create an instance to get my global listener using getTag(). I think this way is a way to modularize the listening of the markers separately, unlike when we use googleMap.setOnMarkerClickListener(this) and a single overridden method onMarkerClick for all markers.

@Override
public boolean onMarkerClick(Marker marker) {
    GoogleMap.OnMarkerClickListener listener = (GoogleMap.OnMarkerClickListener) marker.getTag();
    //I used listener!=null to avoid an exception
    return listener != null && listener.onMarkerClick(marker);
}

This way you would have to modify onMapReady and add global listeners as many times as markers need. The last method I wrote will always be the same.

Upvotes: 0

Nofal Idrees
Nofal Idrees

Reputation: 11

You can also do it like...

   String Title = marker.getTitle();
          Log.e("I am in onMarkerClick" , Title);
    if(Title.equals("Pick Me")){
        Toast.makeText(getBaseContext(),filterAddress,Toast.LENGTH_LONG).show();
        Log.e("I am in onMarkerClick", Title);
    }else if (Title.equals("Drop ME")){
        Log.e("I am in onMarkerClick" , Title);
        Toast.makeText(getBaseContext(),DropoffAdress,Toast.LENGTH_LONG).show();
    }

//Title is the markeroptions.Title("Your specified Title");

Upvotes: 0

MaciejGórski
MaciejGórski

Reputation: 22232

There are a couple of ways to do this.

If all your Markers have a value you can distinguish on (e.g. title), you may do this:

String title = marker.getTitle();
if ("User".equals(title)) {
    // show dialog
} else if ("Event".equals(title)) {
    // do thing for events
} else {
    // do thing for nearby places
}

Snippet can be used for this, but you have to take care of creating info window View if you don't want to show snippets like "USER", "EVENT" or "NEARBY_PLACE" constants.

A more general way could be storing all your Markers in Sets (or Maps if you want to assign additional value to every Marker):

private Set<Marker> events;
private Set<Marker> nearbyPlaces;

and later checking if clicked Marker is in one of these sets:

if (user_marker.equals(marker)) {
    // show dialog
} else if (events.contains(marker)) {
    // do thing for events
} else if (nearbyPlaces.contains(marker)) {
    // do thing for nearby places
} else {
    // something is wrong, better call police (assuming there are only 3 kinds of markers)
}

Last but not least, you can use Android Maps Extensions to remove ugly if statements.

Create one OnMarkerClickListener for each Marker type, e.g.:

private OnMarkerClickListener userMarkerClickListener = new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        // show dialog
        return true;
    }
};

private OnMarkerClickListener eventMarkerClickListener = new OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        // do thing for events
        return false;
    }
};

When creating Marker of a given type, assign click listener:

user_marker.setData(userMarkerClickListener);

In your global OnMarkerClickListener, which is added to GoogleMap object call:

@Override
public boolean onMarkerClick(Marker marker) {
    OnMarkerClickListener listener = (OnMarkerClickListener) marker.getData();
    return listener.onMarkerClick(marker);
}

If you have only 3 kinds of Markers, I would go for the second option, because it is simple and robust. If more than 5 or likely to change in the future, object-oriented code created using third option will be easier to maintain IMHO.

Upvotes: 7

Related Questions