Ken
Ken

Reputation: 295

Add Image to Google Maps Marker Title in Android

I dont want to change the marker's image I just want to add an image to the title so it looks nice

something like this "Read More" image..

enter image description here

is there any way to do so

thanks

Upvotes: 1

Views: 2193

Answers (1)

skywall
skywall

Reputation: 4005

Title is called InfoWindow. To achieve your custom window, you need:

1) Create class which implement GoogleMap.InfoWindowAdapter interface. Something like this (in this case just TextView):

public class MapItemAdapter implements GoogleMap.InfoWindowAdapter {

  TextView tv;

  public MapItemAdapter(Context context) {
      tv = new TextView(context);
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
      tv.setLayoutParams(lp);
      tv.setGravity(Gravity.CENTER);
  }


  @Override
  public View getInfoWindow(Marker marker) {
      tv.setText(marker.getTitle());
      return tv;
  }

  @Override
  public View getInfoContents(Marker marker) {
      return null;
  }
}

2) Set adapter to GoogleMap you've received in onMapReady() callback:

@Override
public void onMapReady(final GoogleMap map) {
    ...
    map.setInfoWindowAdapter(new MapItemAdapter(this));
    ...
}

Upvotes: 2

Related Questions